合并两组数据,然后在Laravel中排序和分页

时间:2019-03-14 05:32:05

标签: laravel eloquent

我正在运行两组查询。但是问题是我无法对它们进行排序和分页。 我的代码如下。

$orderhenkans = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                               ->where('kokyakuorderbango',4)
                               ->get()->pluck('bango'); 
$misyukko1 = Misyukko::whereIn('orderbango',$orderhenkans)
                             ->whereNotNull('yoyakubi')->get();

$orderhenkans = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                              ->get()->pluck('bango');
$misyukko2 = Misyukko::whereIn('orderbango',$orderhenkans)
                               ->whereNotNull('yoyakubi')
                               ->whereDate('hanbaibukacd','<',date('Y-m-d'))->get();

$misyukkos = $misyukko1->merge($misyukko2)->orderBy('orderbango', 'desc')->paginate(15);

我正在尝试从同一模型中获取两个集合。 $misyukko1$misyukko2都给了我正确的输出。之后,我将它们合并到$misyukkos变量中。

当我尝试dd($misyukkos)时,也会给我正确的结果。但是,我不能像平常一样应用orderBy和分页。如何分类和分页数据?

可能值得注意的是Orderhenkan模型具有主键。但是Misyukko没有。

P.S。我在论坛上搜索。我发现了一些类似于我的问题。但是解决方案对我不起作用。因此,请勿将其标记为重复。

4 个答案:

答案 0 :(得分:1)

尝试一下:

$misyukkos = $misyukko1->merge($misyukko2);
$misyukkos->sort()->paginate(15);

答案 1 :(得分:1)

您可以将查询更新为单个查询:建议

尝试一下:

$orderhenkans = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                               ->where('kokyakuorderbango',4)
                               ->get()->pluck('bango'); 


$orderhenkans2 = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                              ->get()->pluck('bango');

$misyukkos = Misyukko::where(function($query) use ($orderhenkans, $orderhenkans2){
                            $query->where(function($q) use ($orderhenkans){
                                $q->whereIn('orderbango',$orderhenkans);
                            })
                            ->orWhere(function($q) use ($orderhenkans2){
                                return $q->whereIn('orderbango',$orderhenkans2)
                                         ->whereDate('hanbaibukacd','<',date('Y-m-d')); //or \Carbon\Carbon::now()->format('Y-m-d')
                            });
                        })
                        ->whereNotNull('yoyakubi')
                        ->orderBy('orderbango', 'desc')
                        ->paginate(15);

答案 2 :(得分:0)

是的,您还可以更新为单个查询。

$orderhenkans = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                               ->where('kokyakuorderbango',4)
                               ->get()->pluck('bango'); 

$orderhenkans_2 = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                              ->get()->pluck('bango');

$misyukkos = Misyukko::where(function($query) use ($orderhenkans, $orderhenkans_2){
    return $query->where(function($query_2) use ($orderhenkans){
        return $query_2->whereIn('orderbango',$orderhenkans)
                       ->whereNotNull('yoyakubi');
    })
    ->orWhere(function($query_2) use ($orderhenkans_2){
        return $query_2->whereIn('orderbango',$orderhenkans_2)
                        ->whereNotNull('yoyakubi')
                        ->whereDate('hanbaibukacd','<',date('Y-m-d'));
    });
})->orderBy('orderbango', 'desc')->paginate(15);

编辑

$orderhenkans = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                                   ->where('kokyakuorderbango',4)
                                   ->get()->pluck('bango'); 

    $orderhenkans_2 = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
                                  ->get()->pluck('bango');

    $misyukkos = Misyukko::where(function($query) use ($orderhenkans, $orderhenkans_2){
        return $query->where(function($query_2) use ($orderhenkans){
            return $query_2->whereIn('orderbango',$orderhenkans);
        })
        ->orWhere(function($query_2) use ($orderhenkans_2){
            return $query_2->whereIn('orderbango',$orderhenkans_2)                            
                            ->whereDate('hanbaibukacd','<',date('Y-m-d'));
        });
    })->whereNotNull('yoyakubi')->orderBy('orderbango', 'desc')->paginate(15);

答案 3 :(得分:0)

 for (var j=0; j<20; j++) {
     this["row"+j] = [];
     for (var i=0; i<10; i++) {
         ["row"+j].push("false");
     }
 }
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;