*/ protected $fillable = [ 'seo_title', 'seo_description', 'slug', 'title', 'tips', 'picture', 'picture_alt', 'picture_title', 'advice', 'preparation', 'ingredients_list', 'video_id', 'consumption_moment_id', 'product_id', 'preparation_time', 'cooking_time', 'nb_guests', 'difficulty', 'highlight', 'published', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'ingredients_list' => 'array', 'difficulty' => DifficultyEnum::class, 'highlight' => 'boolean', 'published' => 'boolean', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::assertTitleIsUnique($model->title, $model->getKey()); self::setHighlightValue($model); self::manageFields($model); }); static::updating(function (self $model) { self::assertTitleIsUnique($model->title, $model->getKey()); self::setHighlightValue($model); self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); }); } // * METHODS /** * Manage model fields. * * @param self $model * @return void */ private static function manageFields(self $model): void { $model->slug = Str::slug($model->title); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); } /** * Asserts using validation that the label is unique * * @param string|null $title * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If a range title already exists. */ private static function assertTitleIsUnique(string|null $title, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id); } /** * Manage model fields. * * @param self $model * @return void */ public static function setHighlightValue(self $model): void { /** @var \App\Models\Recipe|null $recipeOld */ $recipeOld = Recipe::query()->where('highlight', true)->first(); if ($recipeOld && $recipeOld->getkey() !== $model->getkey() && $model->highlight) { $recipeOld->highlight = false; $recipeOld->saveQuietly(); } } /** * Convert minutes times format to hours and minutes times format. * * @param integer $time * @return string */ public static function convertTime(int $time): string { return sprintf( '%s%s', floor($time / 60) ? floor($time / 60) . 'h' : '', // @phpstan-ignore-next-line (($time - floor($time / 60) * 60 < 10) && floor($time / 60) > 0 ? '0' : '') . $time - floor($time / 60) * 60 . 'min', ); } /** * Transform to seoModel. * * @return \Uwil\Seo\Laravel\Objects\SeoItem */ public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem { return new SeoItem( 'recipe', $this->seo_title, $this->seo_description, $this->published ?? false, \route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this), route('fo.recipes.show', $this->slug) ); } // * RELATIONSHIPS /** * Get all of the consumption moment for the recipe. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function consumptionMoment(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(ConsumptionMoment::class); } /** * Get the video for the recipe. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function video(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Video::class); } /** * Get all of the products for the recipe. * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function products(): \Illuminate\Database\Eloquent\Relations\MorphToMany { return $this->morphToMany(Product::class, 'productable')->with(['meat', 'submeat']); } }