@saidbakr here you go Illuminate\Database\Eloquent\ModelNotFoundException and setModel() method
How to translate No query results for model message
When a model id is not found, I got custom 404 error with message says "No query results for model [App\ModelName]" I need to translate this message.
I searched in resources/lang/en to find any file that may contain this message, but I could not find any one. It is just four files there, auth.php, pagination.php, passwords.php and validation.php.
Capture the ModelNotFoundException inside app/Exceptions/Handler.php and handle it with your own translated message.
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
// a JSON response for example
return response()->json(['message' => trans('exceptions.model_not_found')], 404);
}
Are you sure that message is displayed if you are in production mode?
Normally, 404's are returned as "Sorry, the page you are looking for could not be found."
This error view can be overwritten with your own custom view
@tykus , in my case, it is not JSON:
public function render($request, Exception $exception)
{
if ($exception instanceof ModelNotFoundException) {
$exception = new NotFoundHttpException($exception->getMessage(), $exception);
}...
```
@Snapey Yes it is in production because, APP_DEBUG is set to false and APP_ENV is set to production. However, it also works in dev mode.
In that case, just translate the message yourself and pass it to the newly thrown exception:
$exception = new NotFoundHttpException(trans('exceptions.model_not_found'), $exception);
You can put a placeholder (:model) in the translation and pass the Model name from the original ModelNotFoundException (if you need to):
$exception = new NotFoundHttpException(
trans('exceptions.model_not_found', ['model' => $exception->getModel()]),
$exception
);
Well, I never see 'model not found' error when in production? Why would you?
I just see a 404 page
It seems that if ($exception instanceof ModelNotFoundException not evaluated! because any change to the following line does not give any cahnges such as:
$exception = new TokenMismatchException($exception->getMessage(), $exception);
My action works like the following:
public function edit(Eqtype $eqtype)
{
return view('eqtype.edit',['model' => $eqtype, 'actionPath' => '/eqtype/update/'.$eqtype->id]);
}
I think that it gives sense when you require eqtype/edit/1000 and there is no an eqtype with the id 1000, to invoke 404 error.
Did you import ModelNotFoundException in the Handler.php file?
@tykus I think it is not imported:
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if ($exception instanceof ModelNotFoundException) {
$exception = new NotFoundHttpException($exception->getMessage(), $exception);
}
```
So import it, or use the FQCN in the render method:
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException::class) {
$exception = new NotFoundHttpException($exception->getMessage(), $exception);
}
@tykus The most weird thing here, is the output of dd($exception,$exception instanceof ModelNotFoundException)
public function render($request, Exception $exception)
{
dd($exception,$exception instanceof ModelNotFoundException);
if ($exception instanceof ModelNotFoundException) {
$exception = new NotFoundHttpException($exception->getMessage(), $exception);
}
It returns the following:
ModelNotFoundException {#722 ▼
#model: "App\Product"
#ids: []
#message: "No query results for model [App\Product]."
#code: 0
#file: "/home/said/www/factory/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php"
#line: 395
trace: {▶}
}
false
That means $exception instanceof ModelNotFoundException should return true not false!
Did you import ModelNotFoundException in Handler? If you do not, then your are checking if the exception is an instance of App\Exceptions\ModelNotFoundException, which it is not.
Please or to participate in this conversation.