本文介绍了Laravel 5.7 按日期搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:如何按日期和描述搜索?我的问题是什么?我认为它可以工作,但它根本不起作用.

控制器

公共函数索引(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')->分页(10)->withPath('?search='.$search);return view('projects.index', compact('projects'));

查看

<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">搜索</button>

解决方案

你可以使用 ->whereDate() 而不是 ->where()>

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

如果上面不起作用,那么试试这个

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

检查这个链接1这也是 Link2

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'));

View

<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>
解决方案

You can use ->whereDate() instead of ->where()

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

if above not working then try this

$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);

check this Link 1and this also Link2

这篇关于Laravel 5.7 按日期搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 16:30