0
votes

I want to make Laravel Authorization using gate..Where In user model

User.php

 public function hasPermission($name)
    {
       $permission = Permission::where('name','=', $name)->first();
        $permissions = \DB::table('role_permission')
            ->join('permissions', 'permissions.id', '=', 'role_permission.permission_id')
            ->select('role_permission.*')
            ->where('role_permission.permission_id', '=',$permission->id)
            ->get();  

        if(! $permissions) {
            return false;
        }

        return !! $permission->intersect($this->$permission)->count();

    }

In AuthserviceProvider

 public function boot(GateContract $gate)
        {
            $this->registerPolicies($gate);

            $gate->before(function($user, $ability) {

                  return $user->hasPermission($ability);
            });

        }

My Table structure like.

User has name,email,password,id permission has name,id role has name,id role_permission has role_id,permission_id

can anyone help me to find out what's the error here?

2

2 Answers

0
votes

intersect method belongs to Collection class. You can't use intersect method on Model. You may try by:

return !! collect([$permission])->intersect($this->$permission)->count();

$this->$permission should be an array or collection

0
votes

I guess that's because you can apply intersect to a collection, while you're getting an error saying you that you're calling it over Illuminate\Database\Query\Builder.

I see you're calling it on $permission, that's currently a record (a model) of Permission. Probably that's just a typo, and you want to use $permissions instead of $permission.

Anyway, try to explain better what's the behaviour you're looking for, because it's not clear.