Laravel No Query Results for Model

Laravel No Query Results for Model

Laravel no query results; In this tutorial, you will learn how to solve no query results error in laravel.

Sometimes, you are building a restful API in laravel and you find “No query results for the model [App\User], [App\Pruduct], [App\Model\User] etc”, This type of error comes in one condition. There is that if you want to fetch data from laravel model and data is not found in database. So at that time, returns ModelNotFoundException exception as response.

So note that, To handle exception and return a better response. So just you need to use handle ModelNotFoundException in laravel applications.

How to fix Laravel No query results for model

Suppose, you want to fetch all products from MySQL database using laravel eloquent; as shown below:

<?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(Product $product)
    {
        return response()->json($product->toArray());
    }
}

And you are finding some error like the following:

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

So visit the app/Exceptions directory of your laravel application and open Handler.php file; Then update the render method of Handler.php file; as shown below:

<?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.
    *
    * @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.
    *
    * @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);
    }
}

Note that, this example tutorial will also work with laravel version.

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *