for the immediate solution i have created the raw query
// start range 7 days ago
$start = date('z') + 1 - 7;
// end range 7 days from now
$end = date('z') + 1 + 7;
$customerslist = Customers::whereRaw("DAYOFYEAR(birthday) BETWEEN $start AND $end")->get();
EDITED
So i have Created the 5000 records with the random year and date
MethodOne Using Eloquent filter
$checkRange = array_map(
function ($date) {
return $date->format('Y-m-d');
},
\Carbon\CarbonPeriod::create(now(), now()->addDays(7))->toArray()
);
$carbonWay = Customer::get()->filter(function($eachCus) use ( $checkRange){
return in_array( $eachCus->dob, $checkRange);
});
But if you have Many Customer it will heatup the sql server
Method Two QueryBuilder Way
$eloquentway = Customer::whereDate('dob','>=', now())
->whereDate( 'dob', '<=', now()->addDays(7))
->get();
So both results in the Same Results in the same for me
Finaly i have Checked if there the ids are mathes so
$idsofCarbon = $carbonWay->pluck('id')->toArray();
$idsofFilter = $elequentway->pluck('id')->toArray();
dump(array_diff( $idsofFilter, $idsofCarbon));
dump(array_diff( $idsofCarbon, $idsofFilter));
Both are giving me the []
Which means that the result are accurate
For more about date filtering
https://laraveldaily.com/eloquent-date-filtering-wheredate-and-other-methods/
https://laravel.com/docs/5.8/queries#where-clauses
Kindly Comment if any issues
Hope it helps