搜索功能在搜索结果页面Laravel

时间:2018-08-06 07:24:54

标签: laravel laravel-5.6

搜索后,转到结果页面。就我而言:

Route::post('/search', 'Frontend\FrontController@filterSearch');

在该页面中,存在与其他页面相同的搜索表单。其他所有页面搜索过滤都可以正常工作,没有任何问题。但是,当我尝试从结果页面进行搜索(在搜索了某些内容之后)时,我必须选择所有字段才能使搜索过滤结果。在其他页面中,我不必选择所有字段。这是我的控制器:

public function filterSearch(Request $request){

//imports
$categories = Category::all();
$languages = Language::all();
$years = Year::all();

//partials menu variable
$menu = Menu::all();

$movie = Movie::query();

// Search for a movie based on its name.
if ($request->name != null) {
    $movie->where('name', 'LIKE', '%' . $request->name . '%');
}

// Search for a movie based on the year.
if ($request->year != null) {
    $movie->where('year_id', '=', $request->year);
}

// Search for a movie based on the language.
if ($request->language != null) {
    $movie->where('language_id', '=', $request->language );
}

// Search for a movie based on the review.
if ($request->review != null) {
    $movie->where('review', 'LIKE', '%' . $request->review . '%');
}

// Get the results and return them.
$filtered = $movie->latest()->paginate(20);

//return $request->language;
return view('frontend/search/filter-search', compact('filtered', 'menu', 'categories', 'languages', 'years'));


}

这是表格(其他页面都一样):

{!! Form::open(['method' => 'POST', 'action' => 'Frontend\FrontController@filterSearch', 'class' => 'form-style-1']) !!}                            

<div class="row">
      <div class="col-md-12 form-it">
      <label>Enter Name</label>
      <input name="name" type="text" placeholder="Enter Name">
</div>

<div class="col-md-12 form-it">
       <label>Language</label>
       <div class="group-ip">
       <select name="language" class="ui fluid dropdown">
          <option>Filter Language</option>
             @foreach($languages as $language)
          <option value="{{$language->id}}">{{ucfirst($language->language)}} 
          </option>
             @endforeach
      </select>
</div>
</div>
<div class="col-md-12 form-it">
      <label>Enter Reviews (In stars e.g 7.5)</label>
      <input name="review" type="text" placeholder="Enter Reviews Amount">
</div>
<div class="col-md-12 form-it">
     <label>Release Year</label>
<div class="row">
  <div class="col-md-12">
         <select name="year">
             <option>Year</option>
                  @foreach($years as $year)
             <option value="{{$year->id}}">{{$year->year}}</option>
                  @endforeach
        </select>
</div>
</div>
</div>
<div class="col-md-12 ">
     <input class="submit" type="submit" value="Search">
</div>
</div>
{!! Form::close() !!}

在这里,如果我使用名称“ the”进行搜索,而仅使用年份而不将语言留空,则搜索将按预期进行。但是在结果页上,如果我执行相同操作,除非我填写了所有字段,否则它没有任何结果。包括语言。

1 个答案:

答案 0 :(得分:0)

此问题已解决。问题是在搜索结果页面上,我有一些字段未返回空值。我使用dd($request->all())来查看是否所有内容都返回null。但是某些字段未返回空值,这就是问题所在。我忘记将value=“”放在第一个选项上。