Laravel试图获得非对象雄辩关系的财产

时间:2014-03-14 21:46:38

标签: php laravel laravel-4 eloquent relationships

您好我正在laravel的任务表中检索任务。每个任务都有一个相关的status_id,这些状态保存在状态表中,用户也可以创建自己的状态。

状态表

id | user_id | status_name
1  |  1      | low
2  |  1      | high

任务表。

id | user_id |status_id | task_name | task_brief
1  |  1      |2        | taskone  | brief
2  |  1      |1        | tasktwo  | brief

我想检索所有用户任务,每个任务都有一个状态,例如开发,测试,进行中等。这由status_id反映在任务表中。我正在尝试检索状态名称,以便我可以在仪表板上显示它。我的查询是正确的,因为我使用以下方法对其进行了测试:

{{ '<pre>',print_r($user),'</pre>' }}

收到的数据是正确的,从任务到用户到状态db详细信息。但是,我在foreach中检索数据的方式似乎有问题,因为我收到以下错误:

试图获取非对象的属性

我不确定最新情况如何,但我的代码在下面,如果有人可以提供帮助,我会很感激,因为我的知识非常有限。

UserController.php

public function opentasks() {

    $user = User::with(array('tasks', 'tasks.status'))->find(Auth::user()->id);
    return View::make('users.opentasks')->with('user', $user);

}

用户/ 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

public function status(){
        return $this->hasOne('Status', 'id', 'status_id');
    }

user.php的

public function tasks(){
    return $this->hasMany('Task', 'user_id');
    }

routes.php文件

{
    Route::any("user/opentasks", [
    "as"   => "user/opentasks",
    "uses" => "UserController@opentasks"
]);

修改

表架构

任务表 enter image description here

状态表

enter image description here

错误记录

[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} [] []

1 个答案:

答案 0 :(得分:3)

试试这个

@foreach($user->tasks as $task) 
  {{ $task->task_name }} 
  {{ $task->task_brief}} 
  @if(!is_null($task->status)) 
    {{ $task->status->status_name }} 
  @endif 
@endforeach

返回时有一些空值,因此在尝试解析之前请检查它 此外,您应该在task.status上使用belongsTo关系。

古德勒克。