Laravel 10 - How To Handle "No Query Results For Model" Error

29-May-2023

.

Admin

Laravel 10 - How To Handle "No Query Results For Model" Error

Hello Friends,

In this article we will cover on how to implement laravel 10 - how to handle "no query results For model" error. this example will help you how to solve no query results For model error in laravel 10. if you have question about laravel 10 no query results For model then I will give simple example with solution. you can see how to handle error in laravel 10.

Yesterday, I was just working on my restful API with the User model and I found the following error on the show method: "No query results for the model [App\\User] 1", first I search on Google for No query results for a model issue and fix that. but I want to understand why this error comes. I found why No query results for the model are an error on the show, edit, and update method when you used a model as a parameter. So basically when you pass the id in the URL the laravel model will check id is found or not on the given model. So it returns the ModelNotFoundException exception. we can return as a response.

We can simply handle exceptions and return a better response. So just we will handle ModelNotFoundException on our Handler.php file. So you need to just add following code to your Handler.php.

I am going to give you the full error so let's understand what I write and what is the issue I fetched and how to resolve that.

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app

Step 2: Add Controller

php artisan make:controller ModelController

app/Http/Controllers/ModelController.php

<?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());

}

}

My Error :

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

Step 3: Handle Exception

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.

*

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

}

}

I hope it can help you...

#Laravel 10