Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

saidbakr's avatar

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.

0 likes
13 replies
rin4ik's avatar

@saidbakr here you go Illuminate\Database\Eloquent\ModelNotFoundException and setModel() method

tykus's avatar

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);
}
saidbakr's avatar

@tykus , in my case, it is not JSON:

public function render($request, Exception $exception)
    {
        if ($exception instanceof ModelNotFoundException) {
            $exception = new NotFoundHttpException($exception->getMessage(), $exception);
        }...
    ```
saidbakr's avatar

@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.

tykus's avatar

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
);
Snapey's avatar

Well, I never see 'model not found' error when in production? Why would you?

I just see a 404 page

saidbakr's avatar

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]);
    }

@tykus

@Snapey

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.

tykus's avatar

Did you import ModelNotFoundException in the Handler.php file?

1 like
saidbakr's avatar

@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);
        }
    ```

tykus's avatar

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);
}
saidbakr's avatar

@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!

tykus's avatar
tykus
Best Answer
Level 104

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.