【问题标题】:laravel 5.8 echoing out without searching anythinglaravel 5.8 回显而不搜索任何内容
【发布时间】:2020-06-18 15:37:30
【问题描述】:

我有一个小问题,即使没有搜索键也会显示搜索结果。这是sn-p。 这是视图:

<form action="/search" method="GET">
<div class="form-group search-location">
    <input type="text" name="cityKey" id="cityKey" value="{{ request()->input('cityKey') }}"
           class="form-control" >

</div>
<div class="form-group search-info">

    <input type="text" name="key" id="key" value="{{ request()->input('key') }}"
           class="form-control" >

</div>
<button type="submit" class="btn btn-primary search-btn"><i class="fas fa-search"></i>
    <span>search</span></button>

这里是控制器:

public function search(Request $request){
    $cityKey = $request->cityKey;
    $key = $request->key;


    $doctors = Doctor_list::where('speciality_title', 'LIKE', '%' . $key . '%')->
    where('location', 'LIKE', '%' . $cityKey . '%')->
    orWhere('doctors_name', 'LIKE', '%' . $key . '%')->
    where('location', 'LIKE', '%' . $cityKey . '%')->
    orWhere('speciality_type', 'LIKE', '%' . $key . '%')->
    where('location', 'LIKE', '%' . $cityKey . '%');

//完成查询并用 paginate 或 ->get() 终止它 $doctors = $doctors->get();

    return view('search', compact('doctors'));


}

【问题讨论】:

  • 你想要什么。稍微解释一下
  • 你可以通过验证来检查keycityKey是否不为空..

标签: php mysql laravel laravel-5.8


【解决方案1】:

解决方案很简单,只是不要将 $doctors 变量传递给 view(或者更好地说是 pass 和 empty var)并在 view 中检测到它的空并说没有搜索结果。这是代码:

public function search(Request $request){
    $cityKey = $request->cityKey;
    $key = $request->key;

    if (filled($cityKey) && filled($key)) {
        $doctors = Doctor_list::where('speciality_title', 'LIKE', '%' . $key . '%')->
        where('location', 'LIKE', '%' . $cityKey . '%')->
        orWhere('doctors_name', 'LIKE', '%' . $key . '%')->
        where('location', 'LIKE', '%' . $cityKey . '%')->
        orWhere('speciality_type', 'LIKE', '%' . $key . '%')->
        where('location', 'LIKE', '%' . $cityKey . '%')->
        get();
    }
    return view('search', [
        'doctors' => $doctors ?? []
    ]);
}

有了这个你甚至不查询数据库。填充是一个 laravel 辅助函数,返回给定值是否不是“空白”链接:https://laravel.com/docs/helpers#method-filled

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多