original_string: 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);
                          }
                      });
                  });
              }
          });
      }
code: 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);
                          }
                      });
                  });
              }
          });
      }
code_tokens: 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 ) ; } } ) ; } ) ; } } ) ; }
docstring: 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
docstring_tokens: Scope the model query to certain permissions only .