使用一对多关联模型时 ,当没有查询到数据时要怎么处理才不会报错

代码块
使用一对多关联模型时 ,当没有查询到数据时要怎么处理才不会报错
报错信息

{
    "message": "Trying to get property 'teacher' of non-object",
    "exception": "ErrorException",
    "file": "/home/vagrant/xes/app/Http/Controllers/Api/CourseController.php",
    "line": 13,
    "trace": [
        {
            "file": "/home/vagrant/xes/app/Http/Controllers/Api/CourseController.php",
            "line": 13,
            "function": "handleError",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        },
        {
            "function": "index",
            "class": "App\\Http\\Controllers\\Api\\CourseController",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
            "line": 54,
            "function": "call_user_func_array"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
            "line": 45,
            "function": "callAction",
            "class": "Illuminate\\Routing\\Controller",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 219,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\ControllerDispatcher",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 176,
            "function": "runController",
            "class": "Illuminate\\Routing\\Route",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 681,
            "function": "run",
            "class": "Illuminate\\Routing\\Route",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 130,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/app/Http/Middleware/Api/RefreshTokenMiddleware.php",
            "line": 30,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "App\\Http\\Middleware\\Api\\RefreshTokenMiddleware",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/medz/cors/src/Laravel/Middleware/ShouldGroup.php",
            "line": 23,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Medz\\Cors\\Laravel\\Middleware\\ShouldGroup",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
            "line": 41,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
            "line": 59,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 105,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 683,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 658,
            "function": "runRouteWithinStack",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 624,
            "function": "runRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 613,
            "function": "dispatchToRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 170,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 130,
            "function": "Illuminate\\Foundation\\Http\\{closure}",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
            "line": 27,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
            "line": 63,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/fideloper/proxy/src/TrustProxies.php",
            "line": 57,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Fideloper\\Proxy\\TrustProxies",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/medz/cors/src/Laravel/Middleware/Cors.php",
            "line": 62,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 171,
            "function": "handle",
            "class": "Medz\\Cors\\Laravel\\Middleware\\Cors",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 105,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 145,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 110,
            "function": "sendRequestThroughRouter",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/home/vagrant/xes/public/index.php",
            "line": 55,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        }
    ]
}

数据库
teacher表
使用一对多关联模型时 ,当没有查询到数据时要怎么处理才不会报错
course表
使用一对多关联模型时 ,当没有查询到数据时要怎么处理才不会报错

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
北冥
最佳答案

你试试 with ,要方便一点

3年前 评论
讨论数量: 5
$course = Course::find(2);
$techer = $course->techer ?? [];
3年前 评论
德国科隆街头的大胡子 3年前
bing (楼主) 3年前
北冥

你试试 with ,要方便一点

3年前 评论
optional(Cource::find(2))->teacher;
3年前 评论

手册都不看,withDefault这么好的东西都不用!
模型关联《Laravel 7 中文文档》

3年前 评论
bing (楼主) 3年前
小学毕业生 3年前

$this->belongTo(Teach::class)->withDefault()

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!