I have my views setup more or less like the picture below or can see it on jsfiddle /
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', pact('get_all_user'));
}
I have my views setup more or less like the picture below or can see it on jsfiddle https://jsfiddle/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', pact('get_all_user'));
}
Share
Improve this question
asked Nov 20, 2017 at 3:32
LearnProgrammingLearnProgramming
8061 gold badge13 silver badges42 bronze badges
2 Answers
Reset to default 10This 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 pare 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', pact('get_all_user'));
}
use Carbon\Carbon;
public function index()
{
$start = Carbon::parse($request->start_date);
$end = Carbon::parse($request->end_date);
$get_all_user = User::whereDate('created_at','<=',$end)
->whereDate('created_at','>=',$start)
->get();
return $get_all_user;
}
Postman
Note: use ->get();
Otherwise you will get an error.