raise(new Deleted($group)); }); } /** * Create a new group. * * @param string $nameSingular * @param string $namePlural * @param string $color * @param string $icon * @param bool $isHidden * @return static */ public static function build($nameSingular, $namePlural, $color = null, $icon = null, bool $isHidden = false): self { $group = new static; $group->name_singular = $nameSingular; $group->name_plural = $namePlural; $group->color = $color; $group->icon = $icon; $group->is_hidden = $isHidden; $group->raise(new Created($group)); return $group; } /** * Rename the group. * * @param string $nameSingular * @param string $namePlural * @return $this */ public function rename($nameSingular, $namePlural) { $this->name_singular = $nameSingular; $this->name_plural = $namePlural; $this->raise(new Renamed($this)); return $this; } /** * Define the relationship with the group's users. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function users() { return $this->belongsToMany(User::class); } /** * Define the relationship with the group's permissions. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function permissions() { return $this->hasMany(Permission::class); } /** * Check whether the group has a certain permission. * * @param string $permission * @return bool */ public function hasPermission($permission) { if ($this->id == self::ADMINISTRATOR_ID) { return true; } return $this->permissions->contains('permission', $permission); } }