Laravel 5.7按日期搜索

时间:2018-11-14 03:36:10

标签: laravel laravel-5 eloquent controller

  

问题是:如何按日期和描述进行搜索?我怎么了我认为它可以工作,但根本不工作。

控制器

public function index(Request $request)
{   
    $search = $request->get('search');
    $date = $request->get('date');
    $projects = Project::where('description', 'like', '%' . $search . '%')
      ->orWhere('created_at', '%Y-%m-%d','LIKE', '%'.$date.'%')
      ->orderBy("created_at", 'desc')
      ->paginate(10)
      ->withPath('?search=' . $search);

    return view('projects.index', compact('projects'));

查看

<div class="col-sm-12 form-group">
  <div class="input-group">
   <input class="form-control" name="date" type="date" > 
   <input class="form-control" name="search" type="text"/>
   <div class="input-group-btn">
     <button type="submit" class="btn btn-success">Search</button>
   </div>         
 </div>
</div>

2 个答案:

答案 0 :(得分:1)

您可以使用 whereDate()

...
->whereDate('created_at', '=', $date)
...

,如果您想使用 OR 其中,则

->orWhere(function ($query) use ($date){
    $query->whereDate('created_at', '=', $date);
});

答案 1 :(得分:1)

您可以使用->whereDate()代替->where()

$projects = Project::where('description', 'like', '%' . $search . '%')
   ->whereDate('created_at',$date)
   ->orderBy("created_at", 'desc')
   ->paginate(10)
   ->withPath('?search=' . $search);

如果上述方法无效,请尝试

$projects = Project::where('description', 'like', '%' . $search . '%')
        ->orWhere(function ($query) use ($date) {
          $query->whereDate('created_at',$date);
        })
       ->orderBy("created_at", 'desc')
       ->paginate(10)
       ->withPath('?search=' . $search);

选中此Link 1 这也是Link2