$news * Associated news of the category. */ class NewsCategory extends Model implements TranslatableModel { use ActivityLog; use HasFactory; use HasCache; use HasTranslations; use SoftDeletes; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'name', 'slug', 'color', ]; /** * The translatable columns. * * @var list */ public $translatable = [ 'name', 'slug' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::manageFields($model); }); static::updating(function (self $model) { self::manageFields($model); }); } // * METHODS /** * Manage model fields. * * @param self $model * @return void */ private static function manageFields(self $model): void { // Check unique fields. $slug = Str::slug($model->name); TranslationHelper::assertTranslationFieldIsUnique($model, $model->name, 'name'); TranslationHelper::assertTranslationFieldIsUnique($model, $slug, 'slug', 'name'); $model->setAttribute('slug', $slug); // Check translations. TranslationHelper::validateFallbackTranslations($model); } // * RELATIONSHIPS /** * Associated news of the category. * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function news(): \Illuminate\Database\Eloquent\Relations\MorphToMany { return $this->morphToMany( News::class, 'newsable', 'newsables', 'news_id', 'newsable_id' ); } }