Join WhatsApp ChannelJoin Now

How To Slow “No Query Results For Model” Error in Laravel 10

Hi Dev,

Today, we will show you how to slow “no query results for model” error in laravel 10. This article will give you simple example of how to slow “no query results for model” error in laravel 10. Let’s discuss how to slow “no query results for model” error in laravel 10. In this article, we will implement a how to slow “no query results for model” error in laravel 10.

We can fix this problem this way, you just need to add the following code to your Handler.php file.

Controller Method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ModelController extends Controller
{
    /**
    * Display the specified resource.
    *
    * @param  \App\Models\Model  $Model
    * @return \Illuminate\Http\Response
    */
    public function show(User $user)
    {
        return response()->json($user->toArray());
    }
}

Error

No query results for model [App\\User] 1

app/Exceptions/Handler.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class Handler extends ExceptionHandler
{
    /**
    * A list of the exception types that are not reported.

	Ezoic
    *
    * @var array
    */
    protected $dontReport = [
        //
    ];

    /**
    * A list of the inputs that are never flashed for validation exceptions.
    *
    * @var array
    */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
    * Report or log an exception.
    *
    * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
Ezoic
    *
    * @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 ($e instanceof ModelNotFoundException) {
            return response()->json(['error' => 'Data not found.']);
        }

        return parent::render($request, $exception);
    }
}

I hope it will assist you…

Recommended Posts