在搜索结果中添加选择orderby或sortby过滤的功能.

问题描述 投票:-1回答:1

我如何在搜索结果中添加选择顺序排序?

enter image description here laravel 5.8?

enter image description here

enter image description here

php laravel-5.8
1个回答
0
投票

根据你的评论, 我更新了我的答案:

在前台的选择菜单应该用选择的菜单项更新url,你可以用onchange来实现。

<select onchange="location = this.value;">
    <option value="">Sort By</option>
    <option value="?sortBy=rate" {{ (request('sortBy') == 'rate' ? 'selected=selected' : '') }}>Rate</option>
    <option value="?sortBy=popular" {{ (request('sortBy') == 'popular' ? 'selected=selected' : '') }}>Popular</option>
    <option value="?sortBy=ohirgi" {{ (request('sortBy') == 'ohirgi' ? 'selected=selected' : '') }}>Ohirgi</option>
    <option value="?sortBy=id" {{ (request('sortBy') == 'id' ? 'selected=selected' : '') }}>id</option>
</select>

当选择的时候,会在url中添加排序By=和所选的选项,或者排序By=速率。

写下你的查询,但不要使用->get(),然后你可以在完成后链上额外的选项,然后用->get()或->paginate()终止。

//initial query without terminator
$doctors = Doctor_list::where('speciality_title', 'LIKE', '%'.$key.'%');

//if there is a sort request use it, otherwise use the default sort
switch (request('sortBy')) {
case 'rate':
    $doctors->orderby('rate', 'desc');
    break;
case 'Popular':
    $doctors->orderby('popular', 'asc');
    break;
case 'Ohirgi':
    $doctors->orderby('ohirgi', 'asc');
    break;
default:
    $doctors->orderby('id', 'asc');
    break;
}

//complete the query and terminate it with paginate or ->get()
$doctors = $doctors->paginate();
© www.soinside.com 2019 - 2024. All rights reserved.