Laravel ModelNotFoundException and NotFoundHttpException Controller:
$customer = Customer::findOrFail($id);
Handler.php
$this->renderable(function ( Exception $exception, $request) {
if ($request->wantsJson()){
dd([
$exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException,
$exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
]);
}
});
Output:
array:2 [
0 => false
1 => true
]
Why is ModelNotFoundException false? Please, share if you have any idea?
Because the prepareException method of the Illuminate\Foundation\Exceptions\Handler class converts the ModelNotFoundException to a NotFoundHttpException:
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
I understood. Now how can I get ModelNotFoundException?
Solution:
if ($exception instanceof NotFoundHttpException) {
if ($exception->getPrevious() && $exception->getPrevious() instanceof ModelNotFoundException){
//do something
}
}
Please sign in or create an account to participate in this conversation.