【问题标题】:why i am getting No query results for model in laravel?为什么我在 laravel 中没有得到模型的查询结果?
【发布时间】:2019-02-27 12:43:24
【问题描述】:

当我按数据库表中可用的城市名称搜索时,它会显示结果,但是当我按数据库表中不可用的未知城市搜索时,它会显示 -没有模型 [App\ 的查询结果城市]。我将代码和截图分享给你see error screenshot 实际上,如果在我的数据库表中找不到该城市,我想重定向到 401 页面

这是我的路线

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

这是我的功能

公共函数 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'));

    }

【问题讨论】:

    标签: laravel laravel-5 laravel-5.2 laravel-5.1


    【解决方案1】:

    那是因为您使用的是 firstOrFail() 方法,正如命名的那样,如果没有结果,则会通过抛出异常而失败,该异常将被重新呈现为“模型没有查询结果...”消息。

    如果你想它不要失败,你可以:

    1. 用 try-catch 块包围您的查询,处理异常并返回一些消息
    2. firstOrFail() 方法替换为first()

    【讨论】:

    • $location = City::where('slug',$location_id)->where('status','1')->first();现在我正在尝试获取非对象的属性(查看:/srv/users/serverpilot/apps/esythink/public/local/resources/views/frontend/teacherjobs/by_location.blade.php)
    • 当然你关注这一行:$location = City::where('slug',$location_id)->where('status','1')->firstOrFail(); 并且因为你正在执行 firstOrFail(),所以当位置找不到时你需要处理异常。当对象 $location 不存在时,会出现错误,因此请确保找到 $location。
    【解决方案2】:

    您可以通过修改处理Illuminate\Database\Eloquent\ModelNotFoundException 异常的方式来处理此类错误。在App\Exceptions\Handler 类中,将渲染方法更改为如下所示

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

    在重定向中,您必须输入要重定向到的路由,我输入/error_page 仅用于示例目的。 您可以通过Error Handling'了解更多信息

    不要忘记像 use Illuminate\Database\Eloquent\ModelNotFoundException; 那样导入 ModelNotFoundException

    【讨论】:

      【解决方案3】:

      替换这个:

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

      有了这个:

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

      【讨论】:

      • 我做了和你说的一样的事,但是得到错误尝试获取非对象的属性(查看:/srv/users/serverpilot/apps/esythink/public/local/resources/views/前端/teacherjobs/by_location.blade.php)
      • 你可以 dd( $location );看看什么属性是空的
      【解决方案4】:

      补充 Elias Soares 的答案,这是因为 laravel 会在 firstOrFail 不返回结果时生成“Illuminate\Database\Eloquent\ModelNotFoundException”异常。

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

      如何处理此异常由您决定。如果使用 firstOrFail -> to first() 解决方案

      将 firstOrFail 替换为 first 后,您会收到错误消息:“尝试获取非对象的属性”,这是因为您的查询没有获取任何内容,因此它没有属性。为此,您可以检查查询结果。

      $location = City::where('slug',$location_id)->where('status','1')->first();
      
      !empty($location->attribute)?$location->attribute:null;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        • 2015-05-04
        • 2019-09-16
        • 1970-01-01
        相关资源
        最近更新 更多