mercredi 3 juin 2020

Laravel implicit model binding

In my Laravel 5.1. application I use Implicit Model Binding. But i want to ask how can i provide custom response in case the resource being accessed doesn't exist in the DB? So when the resource is not found i get:

404 Not Found
"class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",

I tried to change the render method in Handler.php under /app/Exceptions with the following code

    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            dd('model not found');
            //$e = new NotFoundHttpException($e->getMessage(), $e);
        }

        return parent::render($request, $e);
    }

Nothing has changed and dd method didn't have any impact. I changed my code into:

    public function render($request, Exception $e)
    {
        if ($e instanceof NotFoundHttpException) {
            dd('model not found');
            //$e = new NotFoundHttpException($e->getMessage(), $e);
        }

        return parent::render($request, $e);
    }

And also nothing changed. i still received the same response when the resource i'm trying to access doesn't exist.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire