Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why i am getting No query results for model in laravel?

When i search by city name which is available in my database table it will show the result but when i search by unknown city which is not available in my database table it says -No query results for model [App\City]. i am sharing you the code and screenshot see error screenshot actually i want to redirect to 401 page if the city is not found in my database table

Here is my route

Route::get('teacher-jobs-by-city/{city}','TeacherJobsController@by_location');

Here is my function

public function by_location($location_id='') {

$data= array();
$location = City::where('slug',$location_id)->where('status','1')->firstOrFail();
$items= Subject::orderBy('id','asc')->get();
$data['location']=$location;

      //$subjects = [''=>'Select Subject'] + Subject::lists('subject_title','id')->toArray();
      //$city = [''=>'Select City'] + City::lists('name','id')->toArray();
        $active_class ='Select Subject to see job Openings';
        return view('frontend.teacherjobs.by_location',compact('active_class','data','items'));

    }
like image 526
ravi Avatar asked Sep 24 '18 02:09

ravi


3 Answers

That's because you are using the firstOrFail() method, that as named, fails if theres no result by throwing an Exception that will be redered as "No query results for model ..." message.

If you want to it don't fail, you can:

  1. Surround your query with a try-catch block, handling the exception and return some message
  2. Replace the firstOrFail() method by first()
like image 61
Elias Soares Avatar answered Nov 15 '22 10:11

Elias Soares


You can handle that kind of error by modifying the way Illuminate\Database\Eloquent\ModelNotFoundException Exception are handle. In the App\Exceptions\Handler class change the render method to look like this

public function render($request, Exception $exception)
{
    if($exception instanceof ModelNotFoundException){
        return redirect("/error_page", 401)->with('error', $e->getMessage());
    }
    return parent::render($request, $exception);
}

Within the redirect your must put the route to which you want to be redirected, I put /error_page just for sample purpose. You can learn more on Error Handling'

Don't forget to import the ModelNotFoundException like this use Illuminate\Database\Eloquent\ModelNotFoundException;

like image 29
Yves Kipondo Avatar answered Nov 15 '22 10:11

Yves Kipondo


Adding on to the answer by Elias Soares, This happens because laravel generates an "Illuminate\Database\Eloquent\ModelNotFoundException" exception if firstOrFail don't return results.

Reference: https://laravel.com/docs/5.8/eloquent#retrieving-single-models

It's upto you how you want to handle this exception. If using firstOrFail -> to first() solution

After replacing firstOrFail with first, You would get the error: "Trying to get property of non object", That's because your query didn't fetched anything, so it won't have attribute. For that you can place a check on the results of your query.

$location = City::where('slug',$location_id)->where('status','1')->first();

!empty($location->attribute)?$location->attribute:null;
like image 35
Farhan Avatar answered Nov 15 '22 08:11

Farhan