*/ use HasFactory, Notifiable, HasRoles, SoftDeletes; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', 'is_protected' => 'boolean', ]; } /** * Get the user's initials */ public function initials(): string { return Str::of($this->name) ->explode(' ') ->map(fn (string $name) => Str::of($name)->substr(0, 1)) ->implode(''); } public function groups() { return $this->belongsToMany(Group::class) ->withTimestamps() ->using(GroupUser::class); } public function hasPermissionThroughGroup($permission) { return $this->groups->flatMap(function ($group) { return $group->permissions; })->contains('name', $permission); } public function getAllPermissionsAttribute() { return $this->getAllPermissions() ->merge($this->groups->flatMap->permissions) ->unique('id'); } public function hasAnyPermission($permissions): bool { return $this->hasPermissionTo($permissions) || $this->groups->contains(function ($group) use ($permissions) { return $group->hasAnyPermission($permissions); }); } }