I am trying to create a query scope which returns the first latest or null.
I created it like this:
public function scopeUpcomingAction()
{
$upcomingAction = $this->actions()->latest()->whereNotNull('completed_at')->take(1);
if ($upcomingAction->exists()) {
return $upcomingAction;
} else {
return null;
}
}
However when I use this scope like this:
return $subject->load([
'tasks' => function($query) {
$query->with('upcomingAction');
}
]);
when upcomingAction is null it gives me an empty array like this:
{
"id": 3,
"created_at": "2018-01-13 19:08:30",
"updated_at": "2018-01-13 19:08:30",
"upcoming_action": []
}
Can this be told to be singular?