From 989082d719e5b2220eb5d7b4ad4846507ce64993 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 19 Feb 2025 03:26:44 +0000 Subject: [PATCH] Handle the not found errors with route model binding Signed-off-by: snipe --- app/Exceptions/Handler.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 2b8eaa3627..80663b47ac 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -122,6 +122,15 @@ class Handler extends ExceptionHandler } + // This is traaaaash but it handles models that are not found while using route model binding :( + // The only alternative is to set that at *each* route, which is crazypants + if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) { + $model_name = last(explode('\\', $e->getModel())); + $route_base = str_plural(strtolower(last(explode('\\', $e->getModel())))); + return redirect() + ->route($route_base.'.index') + ->withError(trans('general.generic_model_not_found', ['model' => $model_name])); + } if ($this->isHttpException($e) && (isset($statusCode)) && ($statusCode == '404' )) {