Laravel 试图获取非对象雄辩关系的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22416315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel trying to get property of non object eloquent relationship
提问by 001221
Hi I am retrieving tasks in my tasks table in laravel. each task has a related status_id, these statuses are held in the statuses table, a user can also create their own statuses.
嗨,我正在 laravel 的任务表中检索任务。每个任务都有一个相关的status_id,这些状态保存在状态表中,用户也可以创建自己的状态。
Status table
状态表
id | user_id | status_name
1 | 1 | low
2 | 1 | high
Task table.
任务表。
id | user_id |status_id | task_name | task_brief
1 | 1 |2 | taskone | brief
2 | 1 |1 | tasktwo | brief
I want to retrieve all the users tasks, each task has a status e.g. Dev, Test, In Progress etc. This is reflected in the tasks table by the status_id
. I'm trying to retrieve the status name so I can show this on a dashboard. My query is correct as I have tested it using the following:
我想检索所有用户任务,每个任务都有一个状态,例如开发、测试、进行中等。这反映在任务表中status_id
。我正在尝试检索状态名称,以便我可以在仪表板上显示它。我的查询是正确的,因为我已经使用以下方法对其进行了测试:
{{ '<pre>',print_r($user),'</pre>' }}
The data received is correct, from the task to the user to the status db details. However there seems to be a problem with the way I am retrieving the data in my foreach
as I am getting the following error:
接收到的数据是正确的,从任务到用户到状态数据库详细信息。但是,foreach
当我收到以下错误时,我检索数据的方式似乎存在问题:
Trying to get property of non-object
试图获取非对象的属性
I'm not sure whats going on but my code is below, would appreciate if somebody could help as my knowledge is quite limited.
我不确定发生了什么,但我的代码在下面,如果有人能提供帮助,我将不胜感激,因为我的知识非常有限。
UserController.php
用户控制器.php
public function opentasks() {
$user = User::with(array('tasks', 'tasks.status'))->find(Auth::user()->id);
return View::make('users.opentasks')->with('user', $user);
}
users/opentasks.blade.php
用户/opentasks.blade.php
@foreach($user->tasks as $task)
{{ $task->task_name }}
{{ $task->task_brief}}
@if($task->status->count() > 0)
@foreach($task->status as $status)
{{ $status->status_name }}
@endforeach
@endif
@endforeach
task.php
任务.php
public function status(){
return $this->hasOne('Status', 'id', 'status_id');
}
user.php
用户名.php
public function tasks(){
return $this->hasMany('Task', 'user_id');
}
Routes.php
路由.php
{
Route::any("user/opentasks", [
"as" => "user/opentasks",
"uses" => "UserController@opentasks"
]);
EDIT
编辑
Table schema
表模式
Tasks table
任务表
Status Table
状态表
Error Logs
错误日志
[2014-03-14 22:01:28] log.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object' in /Applications/XAMPP/xamppfiles/htdocs/iTempus/app/storage/views/118f4221e407fdefc163f47a92f5bbe3:6
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/iTempus/app/storage/views/118f4221e407fdefc163f47a92f5bbe3(6): Illuminate\Exception\Handler->handleError(8, 'Trying to get p...', '/Applications/X...', 6, Array)
#1 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(38): include('/Applications/X...')
#2 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(45): Illuminate\View\Engines\PhpEngine->evaluatePath('/Applications/X...', Array)
#3 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/View.php(100): Illuminate\View\Engines\CompilerEngine->get('/Applications/X...', Array)
#4 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/View.php(81): Illuminate\View\View->getContents()
#5 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Http/Response.php(70): Illuminate\View\View->render()
#6 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php(141): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))
#7 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1414): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View))
#8 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php(156): Illuminate\Routing\Router->prepare(Object(Illuminate\View\View), Object(Illuminate\Http\Request))
#9 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php(126): Illuminate\Routing\Controllers\Controller->processResponse(Object(Illuminate\Routing\Router), 'opentasks', Object(Illuminate\View\View))
#10 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(985): Illuminate\Routing\Controllers\Controller->callAction(Object(Illuminate\Foundation\Application), Object(Illuminate\Routing\Router), 'opentasks', Array)
#11 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#12 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Route.php(80): call_user_func_array(Object(Closure), Array)
#13 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Route.php(47): Illuminate\Routing\Route->callCallable()
#14 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1016): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#15 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(574): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#16 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(550): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#17 /Applications/XAMPP/xamppfiles/htdocs/iTempus/public/index.php(49): Illuminate\Foundation\Application->run()
#18 {main} [] []
2014-03-14 22:01:28] log.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1429
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1050): Illuminate\Routing\Router->handleRoutingException(Object(Symfony\Component\Routing\Exception\ResourceNotFoundException))
#1 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1014): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(574): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#3 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(550): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#4 /Applications/XAMPP/xamppfiles/htdocs/iTempus/public/index.php(49): Illuminate\Foundation\Application->run()
#5 {main} [] []
回答by majidarif
try this instead
试试这个
@foreach($user->tasks as $task)
{{ $task->task_name }}
{{ $task->task_brief}}
@if(!is_null($task->status))
{{ $task->status->status_name }}
@endif
@endforeach
You have some null values on the return, so check for it before trying to parse.
Also, you should be using belongsTo relationship on task.status
.
您在返回时有一些空值,因此请在尝试解析之前检查它。
此外,您应该在 上使用belongsTo 关系task.status
。
Goodluck.
祝你好运。