Skip to content

Instantly share code, notes, and snippets.

@tranghaviet
Created December 16, 2020 09:21
Show Gist options
  • Save tranghaviet/7bcd0fa277c7834a9ad88a942ac0581e to your computer and use it in GitHub Desktop.
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"
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);
}
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