Created
December 16, 2020 09:21
-
-
Save tranghaviet/7bcd0fa277c7834a9ad88a942ac0581e to your computer and use it in GitHub Desktop.
How to custom not found message in laravel instead of "No query results for model"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function render($request, Throwable $exception) | |
{ | |
if ($exception instanceof ModelNotFoundException) { | |
$model = app($exception->getModel()); | |
return \response()->json([ | |
'message' => method_exists($model, 'notFoundMessage') ? $model->notFoundMessage() : 'Resource not found', | |
], 404); | |
} | |
return parent::render($request, $exception); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function notFoundMessage() | |
{ | |
return 'User not found'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment