添加到搜索结果 select orderby 或 sortby 过滤 Laravel

Adding to select orderby or sortby filtering on search result Laravel

如何在搜索结果中添加 select orderby 排序?

laravel 5.8?

好的,根据您的评论,我已经更新了我的回答:

在前端,select 菜单应使用 selected 菜单项更新 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>

当 selected 将添加 ?sortBy= 和选项 selected 到 url。或 ?sortBy=rate

写下您的查询但不要使用 ->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();