我試圖根據價格(從最低到最高,從最高到最低)對db記錄(遊覽)進行排序。我已經分別在db中添加了12個虛擬記錄(價格從100到1200)。下面是我在控制器代碼進行排序查詢:當排序,以最高的最低的選擇,記錄根據db表的價格價格欄中顯示以下順序無法根據價格對記錄進行排序laravel
public function short(Request $request)
{
if ($request->sortby == "lower") {
$query = Tour::query()->orderBy('price', 'asc');
if (!empty($request->country)) {
$query = $query->whereHas('country', function($r) use($request) {
$r->where('countries.name', $request->country);
});
}
if (!empty($request->category)) {
$query = $query->whereHas('category', function($s) use($request) {
$s->where('categories.name', $request->category);
});
}
if ($request->days !== null) {
$days = $request->days;
$days_explode = explode('|', $days);
$query = $query->whereBetween('days', [$days_explode[0], $days_explode[1]]);
}
$query = $query->get();
}
else{
$query = Tour::query()->orderBy('price', 'desc');
if (!empty($request->country)) {
$query = $query->whereHas('country', function($r) use($request) {
$r->where('countries.name', $request->country);
});
}
if (!empty($request->category)) {
$query = $query->whereHas('category', function($s) use($request) {
$s->where('categories.name', $request->category);
});
}
if ($request->days !== null) {
$days = $request->days;
$days_explode = explode('|', $days);
$query = $query->whereBetween('days', [$days_explode[0], $days_explode[1]]);
}
$query = $query->get();
}
return view('public.tour.search')->withResults($query);
}
:
100,1000,1100,1200,200,300,400,500,600,700,800,900
並且當最高到最低的選擇記錄根據分貝表的價格的價格欄顯示在順序如下:
900,800,700,600,500,400,300,200,1200,1100,1000,100
任何人都可以幫助我嗎?
它在我看來好像Laravel試圖將您的數字排序爲字符串。你確定這些數字是保存在數據庫中的一個「INT」列(或任何其他數字類型)嗎? –