jeudi 1 novembre 2018

Globally catch ModelNotFoundException on Laravel 5

I am trying to catch the ModelNotFoundException globally from the app\Exceptions\Handler class, so to not have to check for it on every single controller.

But it does not work, although it works fine from inside a controller:

try {

        $asset = Asset::findOrFail($asset_id);

    } catch (Exception $e) {

        if ($e instanceof ModelNotFoundException)
        {
            $model = explode('\\', $e->getModel());
            return $this->respondWithRecordNotFound(end($model) . ' record not found');
        }

        return $this->respondWithGeneralError($e->getMessage());
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire