6
votes

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'));

    }
4

4 Answers

7
votes

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()
5
votes

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;

2
votes

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;
0
votes

Replace this:

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

With this:

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