2016-08-13 98 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