非静态方法App \ Collaboration :: project()不应该静态调用,假设来自Laravel中不兼容的上下文的$ this

时间:2017-12-17 08:34:42

标签: php mysql laravel-5 controller

我需要在My laravel app中的每个项目上显示项目协作者。这是My Collaborator表,

id  project_id collaborator_id
 1       1           5
 2       2           8
 3       4           5

我在My Collaborator模型中有queryScope,

 public function scopeProject($query, $id)
    {
        return $query->where('project_id', $id);
    }

和协作者方法获取此项目的所有协作者

public function getCollaborators($id)
    {
        $collaborators = Collaboration::project($id)->get();
        return $collaborators;
    }

和路线

Route::get('/project/{project}/collaborators', function($id){ 
       $collaborators = \App\Collaboration::project()->get(); // this is line 150
       $project = \App\Project::findOrFail($id);
       return view('collaborators.form',compact('collaborators','project')); 
    })->name('collaborators.form');

现在当我去展示项目时,会出现以下错误,

ErrorException in routes.php line 150: Non-static method App\Collaboration::project() should not be called statically, assuming $this from incompatible context

但此路线正常运行并显示协作者中的所有协作者

Route::get('/project/{project}/collaborators', function($id){ 
       $collaborators = \App\Collaboration::all();
       $project = \App\Project::findOrFail($id);
       return view('collaborators.form',compact('collaborators','project')); 
    })->name('collaborators.form');

但我需要在每个项目中只显示仅与该项目相关的协作者。如何解决这个问题?

0 个答案:

没有答案