laravel blade foreach将相关任务提取到项目'试图获取非对象的属性'

时间:2014-09-10 21:29:19

标签: laravel foreach eloquent blade

您好我正在尝试将所有相关任务输出到项目但我在错误日志中得到'试图获取非对象的属性'所以我不确定我做错了什么,有没有人有任何想法?

这是我的观点,其中有foreach

{{ $project->project_name }}
    {{ $project->project_brief }}
    {{ $project->clients->client_name }}

   @foreach ($project->tasks as $project)
    {{ $project->tasks->task_name }}
    @endforeach

这是我执行查询的存储库

public function getProjectTasks($project_id) {

    return \Project::with(['clients', 'tasks'])
        ->whereUserId(Auth::user()->id)
        ->find($project_id);
}

这是我的控制者:

public function projectrelatedtasks($project_id)
    {

    // get the Project tasks
        $project = $this->project->getProjectTasks($project_id);
    return View::make('projects.projectrelatedtasks')
            ->with('project', $project);        
        }

但是我收到了这个错误:

Next exception 'ErrorException' with message 'Trying to get property of non-object (View: /media/sf_Sites/tempus/app/views/projects/projectrelatedtasks.blade.php)' in /media/sf_Sites/tempus/app/storage/views/6e987b895513269a7fde1cd2f82fc6a1:12
Stack trace:
#0 /media/sf_Sites/tempus/bootstrap/compiled.php(9742): Illuminate\View\Engines\CompilerEngine->handleViewException(Object(ErrorException))
#1 /media/sf_Sites/tempus/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(56): Illuminate\View\Engines\PhpEngine->evaluatePath('/media/sf_Sites...', Array)
#2 /media/sf_Sites/tempus/bootstrap/compiled.php(9619): Illuminate\View\Engines\CompilerEngine->get('/media/sf_Sites...', Array)
#3 /media/sf_Sites/tempus/bootstrap/compiled.php(9606): Illuminate\View\View->getContents()
#4 /media/sf_Sites/tempus/bootstrap/compiled.php(9597): Illuminate\View\View->renderContents()
#5 /media/sf_Sites/tempus/bootstrap/compiled.php(10295): Illuminate\View\View->render()
#6 /media/sf_Sites/tempus/bootstrap/compiled.php(9826): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))
#7 /media/sf_Sites/tempus/bootstrap/compiled.php(5190): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View))
#8 /media/sf_Sites/tempus/bootstrap/compiled.php(4998): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(Illuminate\View\View))
#9 /media/sf_Sites/tempus/bootstrap/compiled.php(4984): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#10 /media/sf_Sites/tempus/bootstrap/compiled.php(717): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#11 /media/sf_Sites/tempus/bootstrap/compiled.php(698): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#12 /media/sf_Sites/tempus/bootstrap/compiled.php(7706): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#13 /media/sf_Sites/tempus/bootstrap/compiled.php(8309): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#14 /media/sf_Sites/tempus/bootstrap/compiled.php(8256): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#15 /media/sf_Sites/tempus/bootstrap/compiled.php(10895): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#16 /media/sf_Sites/tempus/bootstrap/compiled.php(659): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#17 /media/sf_Sites/tempus/public/index.php(49): Illuminate\Foundation\Application->run()
#18 {main} [] []

有谁知道如何构建这个foreach来获取项目的所有相关任务?

1 个答案:

答案 0 :(得分:1)

您的for循环错误。它应该是

@foreach ($project->tasks as $task)
    {{ $task->task_name }}
@endforeach

您正在遍历所有tasks,因此foreach中的第二个参数是task本身。