Laravel中同一列上的多个排序依据,是否存在任何方法?

jmp7cifd  于 2023-03-09  发布在  其他
关注(0)|答案(4)|浏览(116)

我需要依次从两个查询中获取数据

->when($todayDate, function ($query) use ($todayDate) {
       $query->whereDate('startdate', '>=', $todayDate)->orderBy('batches.startdate', 'desc');
})->when($todayDate, function ($query) use ($todayDate) {
       $query->whereDate('startdate', '<=', $todayDate)->orderBy('batches.startdate', 'desc');
})
juzqafwq

juzqafwq1#

`$todayDate = now()->toDateString();

$upcoming = DB::table('table_name')
->when($todayDate, function ($query) use ($todayDate) {
    $query->whereDate('startdate', '>=', $todayDate)
        ->orderBy('batches.startdate', 'desc');
});

$past = DB::table('table_name')
->when($todayDate, function ($query) use ($todayDate) {
    $query->whereDate('startdate', '<=', $todayDate)
        ->orderBy('batches.startdate', 'desc');
});

$results = $upcoming->union($past)->get();'

'->when($todayDate, function ($query) use ($todayDate) { 
$query->orderBy('startdate', 'desc')
     ->orderByRaw("startdate >= '{$todayDate}' desc");
})`
bzzcjhmw

bzzcjhmw2#

在Laravel中,你可以使用union()方法来合并两个查询的结果。

$todayDate = date('Y-m-d');

$query1 = DB::table('table_name')
    ->when($todayDate, function ($query) use ($todayDate) {
        $query->whereDate('startdate', '>=', $todayDate)
            ->orderBy('batches.startdate', 'desc');
    });

$query2 = DB::table('table_name')
    ->when($todayDate, function ($query) use ($todayDate) {
        $query->whereDate('startdate', '<=', $todayDate)
            ->orderBy('batches.startdate', 'desc');
    });

$results = $query1->union($query2)->get();
smdncfj3

smdncfj33#

$packages1 = Package::select('packages.*')->join('package_prices', 'packages.id', '=', 'package_prices.package_id')
                ->Join('package_itineraries', 'packages.id', '=', 'package_itineraries.package_id')
                ->leftJoin('batches', 'packages.id', '=', 'batches.package_id')
                ->when($states, function ($query) use ($states) {
                    $states = Collection::make(explode(",", $states));
                    $query->whereIn('state_id', $states);
                })
                ->when($countries, function ($query) use ($countries) {
                    $countries = Collection::make(explode(",", $countries));
                    $query->whereIn('country_id', $countries);
                })

                ->when($difficulty, function ($query) use ($difficulty) {
                    $difficulty = Collection::make(explode(",", $difficulty));
                    $query->whereIn('trek_difficulty_id', $difficulty);
                })

                ->when($category, function ($query) use ($category) {
                    $query->where('category_id', $category);
                })
                ->when($prices, function ($query) use ($prices) {
                    $query->priceBetween($prices);
                })
                ->when($batches, function ($query) use ($batches) {
                    $query->batchesBetween($batches);
                })
                ->when($duration, function ($query) use ($duration) {
                    $query->DurationBetween($duration);
                })
                ->when($startingLocations, function ($query) use ($startingLocations) {
                    $query->Startinglocations($startingLocations);
                })
                ->when($columnName, function ($query) use ($columnName, $columnSortOrder) {
                    if ($columnName == "price") {
                        $query->where('package_prices.is_default', 1)->orderBy('package_prices.price', $columnSortOrder);
                    } elseif ($columnName == "duration") {
                        $query->orderBy('package_itineraries.duration', $columnSortOrder);
                    }
                    $query->orderBy($columnName, $columnSortOrder);
                })
                ->when($todayDate, function ($query) use ($todayDate) {
                    $query->whereDate('batches.startdate', '>=', $todayDate)->orderBy('batches.startdate', 'asc');
                });
eblbsuwk

eblbsuwk4#

这就是没有工会我们能做的

$case = "CASE WHEN batches.startdate >= CURDATE() THEN 1 ELSE 2 END";

 ->orderByRaw($case)
                ->orderBy('batches.startdate')

相关问题