I want to filter data between two dates in laravel. I have table with column date where date is stored in Y-m-d format.
$total = App\Models\Data::where('age_id',$age->id)
->where('nationality',$nationality)
->when(isset($to), function($q){
$q->whereBetween('dateTime',[$from,$to]);
})->when(!isset($to), function($q){
$q->where('dateTime',$from);
})
->sum('count');
My condition is that $to sometimes can be null. But whereBetween requires both $from and $to for it to work. And whereIn fetch data of $from and $to but not between $from and $to. If only $from is filled I want to filter data on that requested date. And if $from and $to is filled I want to filter data between $from and $to and perform count on those data. How can I achieve that?