2016-08-13 61 views
0

我有2个公共职能在我的控制器如何在Laravel做到这一点5.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); 

我的公共职能指数()方法打印协作者在('projects.index')视图

这个怎么办?

回答

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”),你会为每个数据库记录

+0

做到了合作者的价值,但发生下列错误未定义的变量:协作者(查看:C:\ Users \ Fernando \ Desktop \ c \ resources \ views \ collaborators \ form.blade.php) – Fernando