*/ protected $fillable = [ 'label', 'subtitle', 'slug', 'description', 'picture', 'picture_alt', 'picture_title', 'meat_id', 'published', 'order', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'published' => 'boolean', ]; /** * Perform any actions required after the model boots. */ protected static function booted(): void { static::creating(function (self $model) { self::assertLabelIsUnique($model->label); self::setOrder($model); self::manageFields($model); }); static::created(function () { self::resetCache(); }); static::updating(function (self $model) { self::assertLabelIsUnique($model->label, $model->getKey()); self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); self::resetCache(); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); self::resetCache(); }); } // * METHODS /** * Manage model fields. */ private static function manageFields(self $model): void { $model->slug = Str::slug($model->label); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); } /** * Asserts using validation that the label is unique * * @throws \Illuminate\Validation\ValidationException If a tange label already exists. */ private static function assertLabelIsUnique(?string $label, ?int $id = null): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id); } /** * Set order after the last element of the list. */ private static function setOrder(self $model): void { $model->order = \intval(self::query()->max('order')) + 1; } /** * Reset all cache elements related to this model. */ private static function resetCache(): void { Cache::forget('publishedSubmeatsOrderedByOrder'); self::getPublishedOrdered(); } /** * Get all published Subeats ordered by order. */ public static function getPublishedOrdered(): Collection { return Cache::remember('publishedSubmeatsOrderedByOrder', 86400, function () { return self::query()->where('published', true)->orderBy('order')->get(); }); } /** * Create a SchemaOrg. */ public function toSchemaOrg(): DefinedTerm { return Schema::definedTerm() ->name($this->label) ->description(strip_tags($this->description)) ->image( Schema::imageObject() ->url(asset($this->picture)) ->headline($this->picture_title) ) ->inDefinedTermSet($this->meat->toSchemaOrg()); } // * REDIRECTIONS. /** * All models it belongs to. * * @return array */ protected function getRouteParentModels(): array { return ['App\Models\Meat']; } /** * Get the front url of this model. * Should return the model.show route, with all binded elements if needed. */ public function getFrontUrl(string $modelKey): array | string { return [ route('fo.products.search.submeat', [ 'model_slug' => $this->meat()->first()->slug, 'child_slug' => $modelKey, ]), route('fo.recipes.search.submeat', [ 'model_slug' => $this->meat()->first()->slug, 'child_slug' => $modelKey, ]), ]; } // * RELATIONSHIPS /** * Get all of the products for the submeat. */ public function products(): HasMany { return $this->hasMany(Product::class); } /** * Get the meat that owns the submeat. */ public function meat(): BelongsTo { return $this->belongsTo(Meat::class); } }