Eloquent firstOrNotFound
#51665
-
Is there a possibility to add this function public function firstOrNotFound(Builder $query): mixed
{
$model = $query->first();
if (!$model) {
throw new NotFoundHttpException();
}
return $model;
} Someone knows how to add that function as global function in my code using eloquent and typed data? Right now I return $user = User::firstOrNotFound(
User::where('email', '[email protected]')
); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Looks like I should use this function User::where('email', '[email protected]')->firstOr(
callback: fn () => throw new NotFoundHttpException(),
); |
Beta Was this translation helpful? Give feedback.
Looks like I should use this function
firstOr
: