laravel blade foreach fetching related tasks to projects 'Trying to get property of non-object'

最后都变了- 提交于 2019-12-25 16:48:17

问题


Hi I am trying to output all the related tasks to a Project but I am getting 'Trying to get property of non-object' in my error log so I'm not sure what I am doing wrong, does anyone have any ideas?

This is my view which has the foreach

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

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

This is my repository which performs the query

public function getProjectTasks($project_id) {

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

and this is my controller:

public function projectrelatedtasks($project_id)
    {

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

but I get this error:

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

Does anyone know how I can construct this foreach to fetch all the related tasks to a project?


回答1:


Your for loop is wrong. It should be

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

Your are looping through all the tasks so the second argument in the foreach is the task itself.



来源:https://stackoverflow.com/questions/25775396/laravel-blade-foreach-fetching-related-tasks-to-projects-trying-to-get-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!