如何在Laravel中将表列值与Auth :: user-> id进行比较

时间:2016-12-02 06:22:33

标签: php mysql laravel-5

我的问题是我使用以下代码来访问不同角色的系统用户。

public function show($id)
 {
     if (Permission::where('status', 1)->where('project_id', $id)->exists()) {
    // if((Permission::where('status', '=', '1')->first()) && (Permission::where('project_id','=',$id)->first())){
        $project = Project::find($id);
        $tasks = $this->getTasks($id);
        $files = $this->getFiles($id);
        $comments = $this->getComments($id);
        $collaborators = $this->getCollaborators($id);
        $permissions = $this->getPermissions($id);
returnview('collaborators.show')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
        }
    else if
        //return('hi');
        (Permission::where('status', 2)->where('project_id', $id)->exists()) {
            $project = Project::find($id);
            $tasks = $this->getTasks($id);
            $files = $this->getFiles($id);
            $comments = $this->getComments($id);
            $collaborators = $this->getCollaborators($id);
            $permissions = $this->getPermissions($id);
             return view('collaborators.manager')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
        }

在我的权限表中有collaborator_id,它与用户表用户ID相同。我需要使用已记录的用户Auth::user->id验证(比较)collaborator_id。在以下脚本中。

if (Permission::where('status', 1)->where('project_id', $id)->exists())

怎么办?

1 个答案:

答案 0 :(得分:0)

只需使用另一个where子句,

Permission::where('status', 1)->where('project_id', $id)->where('collaborator_id', Auth::User()->id)

建议:您可以通过在if和else中避免使用相同/重复的代码来减少代码。