public function scopePermission(Builder $query, $permissions): Builder
    {
        $permissions = $this->convertToPermissionModels($permissions);

        $rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) {
            return array_merge($result, $permission->roles->all());
        }, []));

        return $query->where(function ($query) use ($permissions, $rolesWithPermissions) {
            $query->whereHas('permissions', function ($query) use ($permissions) {
                $query->where(function ($query) use ($permissions) {
                    foreach ($permissions as $permission) {
                        $query->orWhere(config('permission.table_names.permissions').'.id', $permission->id);
                    }
                });
            });
            if (count($rolesWithPermissions) > 0) {
                $query->orWhereHas('roles', function ($query) use ($rolesWithPermissions) {
                    $query->where(function ($query) use ($rolesWithPermissions) {
                        foreach ($rolesWithPermissions as $role) {
                            $query->orWhere(config('permission.table_names.roles').'.id', $role->id);
                        }
                    });
                });
            }
        });
    }
public function scopePermission(Builder $query, $permissions): Builder
    {
        $permissions = $this->convertToPermissionModels($permissions);

        $rolesWithPermissions = array_unique(array_reduce($permissions, function ($result, $permission) {
            return array_merge($result, $permission->roles->all());
        }, []));

        return $query->where(function ($query) use ($permissions, $rolesWithPermissions) {
            $query->whereHas('permissions', function ($query) use ($permissions) {
                $query->where(function ($query) use ($permissions) {
                    foreach ($permissions as $permission) {
                        $query->orWhere(config('permission.table_names.permissions').'.id', $permission->id);
                    }
                });
            });
            if (count($rolesWithPermissions) > 0) {
                $query->orWhereHas('roles', function ($query) use ($rolesWithPermissions) {
                    $query->where(function ($query) use ($rolesWithPermissions) {
                        foreach ($rolesWithPermissions as $role) {
                            $query->orWhere(config('permission.table_names.roles').'.id', $role->id);
                        }
                    });
                });
            }
        });
    }
public function scopePermission ( Builder $ query , $ permissions ) : Builder { $ permissions = $ this -> convertToPermissionModels ( $ permissions ) ; $ rolesWithPermissions = array_unique ( array_reduce ( $ permissions , function ( $ result , $ permission ) { return array_merge ( $ result , $ permission -> roles -> all ( ) ) ; } , [ ] ) ) ; return $ query -> where ( function ( $ query ) use ( $ permissions , $ rolesWithPermissions ) { $ query -> whereHas ( 'permissions' , function ( $ query ) use ( $ permissions ) { $ query -> where ( function ( $ query ) use ( $ permissions ) { foreach ( $ permissions as $ permission ) { $ query -> orWhere ( config ( 'permission.table_names.permissions' ) . '.id' , $ permission -> id ) ; } } ) ; } ) ; if ( count ( $ rolesWithPermissions ) > 0 ) { $ query -> orWhereHas ( 'roles' , function ( $ query ) use ( $ rolesWithPermissions ) { $ query -> where ( function ( $ query ) use ( $ rolesWithPermissions ) { foreach ( $ rolesWithPermissions as $ role ) { $ query -> orWhere ( config ( 'permission.table_names.roles' ) . '.id' , $ role -> id ) ; } } ) ; } ) ; } } ) ; }
Scope the model query to certain permissions only.

@param \Illuminate\Database\Eloquent\Builder $query
@param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions

@return \Illuminate\Database\Eloquent\Builder
Scope the model query to certain permissions only .