如何在Laravel 5.2中做到这一点

时间:2016-08-13 09:44:47

标签: php laravel-5

我在My Controller中有2个公共功能

 public function index()
{
    $projects = Project::personal()->get();
    return view('projects.index')->withProject($projects);
}

另一个是

 public function show($id) {
    $project = Project::find($id);
    $tasks = $this->getTasks($id);
    $files = $this->getFiles($id);
    $comments = $this->getComments($id);
    $collaborators = $this->getCollaborators($id);
    return view('projects.show')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);}

我需要得到

$collaborators = $this->getCollaborators($id);

to my public function index()方法在('projects.index')视图中打印协作者

怎么办呢?

1 个答案:

答案 0 :(得分:0)

使用此代码更新索引函数,尚未测试

public function index()
{
    $projects = Project::personal()->get();

    foreach($projects as $key => $project) {
        $find = Project::find($project->id);
        $tasks = $this->getTasks($project->id);
        $files = $this->getFiles($project->id);
        $comments = $this->getComments($project->id);
        $collaborators = $this->getCollaborators($project->id);   
        $projects[$key]['collaborators'] = $collaborators;
    }

    return view('projects.index')->withProject($projects);
}

在('projects.index')中,您将拥有每个数据库记录的协作者值