*/ use HasFactory; use HasModelNaming; use HasTranslations; use SoftDeletes; /** * The attributes that are mass assignable. * * @var list */ #[\Override] protected $fillable = [ 'name', 'url_re7', 'url_prod', 'project_manager_id', 'gitlab_project_id', ]; /** * The translatable columns. * * @var list */ public $translatable = [ 'name', ]; /** * Perform any actions required after the model boots. */ #[\Override] protected static function booted(): void { static::creating(function (self $model): void { self::manageFields($model); }); static::updating(function (self $model): void { self::manageFields($model); }); } // * METHODS. /** * Manage model fields. */ private static function manageFields(self $model): void { TranslationHelper::assertTranslationFieldIsUnique($model, $model->name, 'name'); if ($model->url_re7) { ToolboxHelper::assertNoInternalUrls('url_re7', $model->url_re7); } if ($model->url_prod) { ToolboxHelper::assertNoInternalUrls('url_prod', $model->url_prod); } } /** * Get relations to include in the cache. * * @return list */ public static function getCacheRelations(): array { return ['projectManager', 'gitlabProject', 'users']; } // * RELATIONS. /** * Get users associated to the site. * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\User,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'> */ public function users(): MorphToMany { return $this->morphedByMany( User::class, 'sitable', 'sitables', 'site_id', 'sitable_id', )->withPivot('created_at'); } /** * Get brands associated to the site. * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Brand,$this,\Illuminate\Database\Eloquent\Relations\MorphPivot,'pivot'> */ public function brands(): MorphToMany { return $this->morphedByMany( Brand::class, 'sitable', 'sitables', 'site_id', 'sitable_id', )->withPivot('created_at'); } /** * Get project manager of the site. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User,$this> */ public function projectManager(): BelongsTo { return $this->belongsTo(User::class, 'project_manager_id'); } /** * Get project manager of the site. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\GitlabProject,$this> */ public function gitlabProject(): BelongsTo { return $this->belongsTo(GitlabProject::class, 'gitlab_project_id'); } }