-
I am starting this conversation as I am unsure if it this something that is supported or a bug.
The issue I am running into is it seems that the cache only store one role at a time because the name is the same. Role.php <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Exceptions\RoleAlreadyExists;
use Spatie\Permission\Models\Role as BaseRole;
use Spatie\Permission\Guard;
class Role extends BaseRole
{
protected static function booted()
{
static::addGlobalScope('organisation', function (Builder $builder) {
if (Auth::user() && Auth::user()->su === 0) {
$builder->where('organisation_id', Auth::user()->organisation_id);
}
});
}
protected $casts = [
'updated_at' => 'datetime:Y-m-d H:i:s',
'created_at' => 'datetime:Y-m-d H:i:s',
];
public function organisation()
{
return $this->belongsTo(Organisation::class);
}
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
if (static::where('name', $attributes['name'])
->where('guard_name', $attributes['guard_name'])
->where('organisation_id', $attributes['organisation_id'])
->first()) {
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
}
return static::query()->create($attributes);
}
} How can I solve the cache issue? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@erikn69 I see you have made a fix for that kind of problem, can you spot anything wrong in my role class? |
Beta Was this translation helpful? Give feedback.
-
First, the laravel-permission/config/permission.php Line 96 in dbc6c18
Second, the name can be the same if you use the teams feature; Otherwise it could be prone to failure. But if you think it's an issue, make a demo app to check |
Beta Was this translation helpful? Give feedback.
First, the
organisation_id
seems liketeam_id
featurelaravel-permission/config/permission.php
Line 96 in dbc6c18
Second, the name can be the same if you use the teams feature; Otherwise it could be prone to failure.
But if you think it's an issue, make a demo app to check
https://spatie.be/docs/laravel-permission/v5/basic-usage/new-app#content-creating-a-demo-app