doufuxing8691 2017-11-20 03:32
浏览 28
已采纳

Laravel,如何在视图中过滤日期?

I have my views setup more or less like the picture below or can see it on jsfiddle https://jsfiddle.net/t37g4yzp/1/

How to filter the date in the table base on the user input Start Date and End date in views?

Controller

public function index()
{
  $get_all_user = User::all();
  return view('userPage.index', compact('get_all_user'));
}

enter image description here

  • 写回答

1条回答 默认 最新

  • duanhai1455 2017-11-20 05:06
    关注

    This is the way I do date filtering. In order to catch the start data and end date inject the Request Object in to the index method.Then I would validate the data submitted and I convert the dates in to Carbon Object which give more options like formatting the dates.lastly I use whereDate eloquent query function to compare dates. So the end result would be something like this.

    public function index(Request $request)
    {
      this->validate($request,[
       'start_date' => 'required|date',
       'end_date' => 'required|date|before_or_equal:start_date',
      ]);
    
      $start = Carbon::parse($request->start_date);
      $end = Carbon::parse($request->end_date);
    
      $get_all_user = User::whereDate('date','<=',$end->format('m-d-y'))
      ->whereDate('date','>=',$start->format('m-d-y'));
    
      return view('userPage.index', compact('get_all_user'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料