我如何在搜索结果中添加选择顺序排序?
laravel 5.8?
根据你的评论, 我更新了我的答案:
在前台的选择菜单应该用选择的菜单项更新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();