$ranges Ranges Has-many relationship. */ class Brand extends Model { use HasFactory; use HasTranslations; use BelongsToTenant; use ActivityLog; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'name', 'color', 'picture', 'status', ]; /** * Translatable fields array * * @var array */ public $translatable = [ 'name', 'slug', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'color' => HtmlColor::class, 'status' => 'bool', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { // * ASSERTIONS self::assertNameIsUnique($model->name); // * TRANSFORMATIONS $model->slug = Str::slug($model->name); /** @phpstan-ignore-next-line */ if ($model->picture instanceof UploadedFile) { $model->picture = ShowroomFileHelper::storeFileWithUUIDName($model->picture, $model); } }); static::updating(function (self $model) { // * ASSERTIONS self::assertNameIsUnique($model->name, $model->id); // * TRANSFORMATIONS $model->slug = Str::slug($model->name); /** @phpstan-ignore-next-line */ if ($model->picture instanceof UploadedFile) { $model->picture = ShowroomFileHelper::storeFileWithUUIDName($model->picture, $model); } }); static::updated(function (self $model) { FileHelper::removeOldFile($model, 'picture'); }); static::deleted(function (self $model) { FileHelper::removeFile($model, 'picture'); }); } // * RELATIONSHIPS /** * Get all of the ranges for the Brand * * @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Range, $this> */ public function ranges(): HasMany { return $this->hasMany(Range::class); } /** * Get all of the products for the Brand * * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough */ public function products(): HasManyThrough { return $this->hasManyThrough(Product::class, Range::class); } // * METHODS /** * Asserts using validation that the name is unique * * @param string|null $name * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If name already exists. */ private static function assertNameIsUnique(string|null $name, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id); } }