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 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真