使用一对多关联模型时 ,当没有查询到数据时要怎么处理才不会报错
代码块
报错信息
{
"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表
你试试 with ,要方便一点