Laravel 5.7按日期搜索 [英] Laravel 5.7 search by date

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

问题描述

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

Question is: How to search by date and by description? What's my problem? I think it could work but it doesn't work at all.

控制器

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>

推荐答案

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

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

检查此链接1 并且 Link2

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆