*/ use GeneratesUniqueTeamSlugs, HasFactory, SoftDeletes; /** * Bootstrap the model and its traits. */ protected static function boot(): void { parent::boot(); static::creating(function (Team $team) { if (empty($team->slug)) { $team->slug = static::generateUniqueTeamSlug($team->name); } }); static::updating(function (Team $team) { if ($team->isDirty('name')) { $team->slug = static::generateUniqueTeamSlug($team->name, $team->id); } }); } /** * Get the team owner. */ public function owner(): ?Model { return $this->members() ->wherePivot('role', TeamRole::Owner->value) ->first(); } /** * Get all members of this team. * * @return BelongsToMany */ public function members(): BelongsToMany { return $this->belongsToMany(User::class, 'team_members', 'team_id', 'user_id') ->using(Membership::class) ->withPivot(['role']) ->withTimestamps(); } /** * Get all memberships for this team. * * @return HasMany */ public function memberships(): HasMany { return $this->hasMany(Membership::class); } /** * Get all invitations for this team. * * @return HasMany */ public function invitations(): HasMany { return $this->hasMany(TeamInvitation::class); } /** * Get all report periods for this team. * * @return HasMany */ public function reportPeriods(): HasMany { return $this->hasMany(ReportPeriod::class); } /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'is_personal' => 'boolean', ]; } /** * Get the route key for the model. */ public function getRouteKeyName(): string { return 'slug'; } }