首页 > 解决方案 > 尝试在 Laravel 中进行多输入搜索

问题描述

我正在尝试制作一个laravel search有 4 列的。我想做的是,如果用户使用一个输入字段进行搜索,他们将获得相关结果,如果用户使用多个搜索输入字段,他们将获得相关结果。

这是我的表格:

<form action="{{ route('properties-list') }}" method="POST" role="search">
   @csrf
   <div class="row">
      <div class="col-lg-3 col-md-6 col-sm-6 col-6">
         <div class="form-group">
            <input style="height: 48px;" id="title" name="title" type="text" class="input-text form-control"
               placeholder="Search by Title">
         </div>
      </div>
      <div class="col-lg-3 col-md-6 col-sm-6 col-6">
         <div class="form-group">
            <select class="selectpicker search-fields" name="type">
               <option value=""> Category </option>
               <option value="Apartments"> Apartments </option>
               <option value="Houses"> Houses </option>
               <option value="Commercial"> Commercial </option>
               <option value="Garages"> Garages </option>
            </select>
         </div>
      </div>
      <div class="col-lg-3 col-md-6 col-sm-6 col-6">
         <div class="form-group">
            <input style="height: 48px;" id="datepicker" name="created_at" type="text"
               class="input-text form-control" placeholder="Date of Posting" autocomplete="false">
         </div>
      </div>
      <div class="col-lg-3 col-md-6 col-sm-6 col-6">
         <div class="form-group">
            <input style="height: 48px;" id="search_term" name="location" type="text" class="input-text form-control" placeholder="Search City,Area">
         </div>
      </div>
   </div>
   <div class="row p-3">
      <div class="col-lg-12 col-md-12 col-sm-6 col-12">
         <div class="form-group">
            <button class="search-button">Search</button>
         </div>
      </div>
   </div>
</form>

这是我的控制器:

public function homeSearch(Request $request)
{
    $title = $request->title;
    $type = $request->type;
    $created_at = $request->created_at;
    $location = $request->location;

if (empty($title) && empty($type) && empty($created_at) && empty($location)) {
    Session::flash('danger', "You didn't select any search any search.");
    return redirect()->back();
}

$properties = Property::where('title', 'LIKE', '%' . $title . '%')
    ->orWhere('type', $type)
    ->orWhere('location', 'LIKE', '%' . $location . '%')
    ->orWhere('created_at', 'LIKE', '%' . $created_at . '%')
    ->get();
dd($properties);

}

如果我曾经搜索标题“Note”,则从以下逻辑:我已经将以下标题保存在我的数据库中

想买Note 10 plus

出售苹果手机

我想卖掉我的note 4

如果我使用 'where' 而不是 'orWhere',我会在我的 dd($properties) 中获得所有 3 个对象,有时它会给我我想要的结果,有时它会返回一个空对象。

这是参考搜索: https ://yts.mx/browse-movies

标签: mysqllaraveleloquentorm

解决方案


使用 when 子句

见:https ://laravel.com/docs/7.x/collections#method-when

$properties = Property::where('title', 'LIKE', '%' . request()->title . '%')
                        ->when(request()->type, function($query) {
                            $query->where('type', request()->type)
                        })
                        ->when(request()->location, function($query) {
                            $query->where('location', 'LIKE', '%' . request()->location . '%')
                        })
                        ->when(request()->created_at, function($query) {
                            $query->where('created_at', 'LIKE', '%' . request()->created_at . '%')
                        })->get();

推荐阅读