dtwxt88240 2018-11-15 20:40
浏览 31

Laravel通过网址排序

I am trying to add simple sorting to my index pages, but I am coming into conflict when I try to include search results or add an additional query to the URL. Here is what I currently have:

Controller

// Search default
if ($request->search == null) {
    $search = '';
} else {
    $search = $request->search;
}

// Sort default
if ($request->sort == null) {
    $sort = 'asc';
} else {
    $sort = $request->sort;
}

$doctors = Doctor::orderBy('last_name', $sort)
    ->where('first_name', 'LIKE', '%' . $search . '%')
    ->orWhere('last_name', 'LIKE', '%' . $search . '%')
    ->orWhere('type', 'LIKE', '%' . $search . '%')
    ->orWhere('npi', 'LIKE', '%' . $search . '%')
    ->orWhere('license', 'LIKE', '%' . $search . '%')
    ->orWhere('dea', 'LIKE', '%' . $search . '%')
    ->paginate(10);

return view('doctors.index')->with('doctors', $doctors);

Blade

{{ Form::open(['method' => 'GET']) }}
    {{ Form::text('search', null, ['class' => 'form-control form-control-sm', 'placeholder' => 'Search Doctors...']) }}
    {{Form::submit('Search', ['class' => 'btn btn-primary btn-sm'])}}
{{ Form::close() }}

@if(Request::input('sort', 'asc') == 'asc')
    <a href="?sort=desc">Name</a>
@else 
    <a href="?sort=asc">Name</a>
@endif

Now, independently, the ?search= and the ?sort= will work. But if I try to search for something, then sort the results, it clears out the search request. I also have other columns such as License#, Address, etc that I want to add so that people can sort by more than just the Name. However, when I change the requests to add a ?sortBy= such as<a href="?sortBy=name?sort=desc">Name</a>, the sort direction no longer works.

  • 写回答

2条回答 默认 最新

  • drogon982007 2018-11-15 20:48
    关注

    The URL query params need to be separated by & not ? like so:

    <a href="?sortBy=name&sort=desc">Name</a>

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错