$breeding_methods Array of Domain\Models\ValueObjects\Products\BreedingMethod * @property \Illuminate\Support\Collection $businesses Array of Domain\Models\ValueObjects\Products\Business * @property \Illuminate\Support\Collection $formats Array of Domain\Models\ValueObjects\Products\ProductFormat * @property string $details_and_advantages * @property string $handling_advices * @property string|null $ingredients * @property string $conservation_before_opening * @property string|null $conservation_after_opening * @property \Domain\Models\ValueObjects\Products\NutritionalValues|null $nutritional_values Domain\Models\ValueObjects\NutritionalValues * @property string|null $pdf * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at * @property-read \Illuminate\Database\Eloquent\Collection $recipes * * @method static \Database\Factories\ProductFactory factory($count = null, $state = []) * * @mixin \Eloquent */ class Product extends Model { /** @use HasFactory<\Database\Factories\ProductFactory> */ use HasFactory; protected $fillable = [ 'seo_title', 'seo_description', 'picture', 'picture_alt', 'picture_title', 'name', 'category', 'technology', 'is_egalim', 'breeding_methods', 'businesses', 'formats', 'details_and_advantages', 'handling_advices', 'ingredients', 'conservation_before_opening', 'conservation_after_opening', 'nutritional_values', 'pdf', ]; protected $casts = [ 'is_egalim' => 'boolean', 'category' => ProductRange::class, 'technology' => Technology::class, 'breeding_methods' => AsEnumCollection::class . ':' . BreedingMethod::class, 'businesses' => AsEnumCollection::class . ':' . Business::class, 'formats' => AsEnumCollection::class . ':' . ProductFormat::class, 'nutritional_values' => NutritionalValuesCast::class, ]; #[\Override] protected static function booted(): void { static::saving(function (self $model): void { self::manageFields($model); }); static::deleting(function (self $model): void { FileStorageHelper::removeFile($model, 'picture'); FileStorageHelper::removeFile($model, 'pdf'); }); } /** @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\App\Models\Recipe,$this> */ public function recipes(): BelongsToMany { return $this->belongsToMany(Recipe::class); } private static function manageFields(self $model): void { self::assertNameIsUnique($model->name, $model->id); $model->slug = Str::slug($model->name); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); if ($model->pdf) { $model->pdf = FileStorageHelper::storeFile($model, $model->pdf, true); } } private static function assertNameIsUnique(?string $name, ?int $id = null): void { $table = new self()->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name ?? ''), $id); } }