Home > Software engineering >  How to sort data between two dates using query() function in laravel
How to sort data between two dates using query() function in laravel

Time:11-07

Here is my controller:

$my_query = Deathregister::query();
foreach ($request->query() as $key => $value) 
{
    $my_query->where($key, $value);
}
$my_query->get();

Now if i pass 'fromdate = 2020-10-30' and 'todate = 2021-11-07' i want all the data in bwtween these days. Is this possible?

CodePudding user response:

You can use CarbonPeriod class of PHP. To use it in Laravel

import it first

use Carbon\CarbonPeriod;

Then in the controller function use

$period = CarbonPeriod::create($request->from_dt,'1 day', $request->to_dt);

foreach ($period as $value) {
   $reqDate =  $value->format('d-M-Y');
   //$reqDate will have the date you can change the format as per your requirement
}

CodePudding user response:

You can use whereDate() method:

Deathregister::whereDate('date_field','>=', $request->input('fromdate'))
    ->whereDate('date_field','<=', $request->input('todate'))
    ->get();
  • Related