I think that this will help better: https://laravel.com/docs/8.x/collections#method-partition
[$active, $inactive] = Survey::orderByDesc('created_at')
->get()
->partition(function ($item) {
if ($item['end_date'] && $item['end_date'] instanceof Carbon)
{
return $item['end_date']->gte(Carbon::now());
}
return false;
});
dd($active->count(), $inactive->count());
I think that I improved your comparison of the date as well, because the way you compared it didn't make sense to me. You were comparing strings. So make sure you compare an instance of Carbon dates. In case $item['end_date'] is not Carbon instance you can make it using Carbon::createFromFormat('Y-m-d', $item['end_date']); for example.