> $ingredients * @property string $teaser * @property string $steps * @property \App\Enums\Recipes\DifficultyEnum $difficulty * @property integer $duration * @property string $tips * @property boolean $status * @property integer $order * @property-read \Illuminate\Support\Carbon $created_at Created date. * @property-read \Illuminate\Support\Carbon $updated_at Updated date. */ class Recipe extends Model implements SeoInterface, SchemaOrgInterface, Sitemapable { use HasFactory; use HasTranslations; use SeoTrait; use SchemaOrgTrait; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'range_id', 'seo_title', 'seo_description', 'name', 'slug', 'picture', 'picture_alt', 'picture_title', 'picture_thumbnail', 'picture_thumbnail_alt', 'picture_thumbnail_title', 'ingredients', 'teaser', 'steps', 'difficulty', 'duration', 'tips', 'status', 'order', 'created_at', 'updated_at' ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'ingredients' => 'array', 'difficulty' => DifficultyEnum::class, ]; /** * The translatable columns. * * @var array */ public $translatable = [ 'seo_title', 'seo_description', 'name', 'slug', 'picture', 'picture_alt', 'picture_title', 'picture_thumbnail', 'picture_thumbnail_alt', 'picture_thumbnail_title', 'ingredients', 'teaser', 'steps', 'tips', ]; // * EVENTS /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { self::assertFieldIsUnique('name', $model->name); self::setSlug($model); self::manageFields($model); self::setOrder($model); }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); FileStorageHelper::removeOldFile($model, 'picture_thumbnail'); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); FileStorageHelper::removeFile($model, 'picture_thumbnail'); }); } // * METHODS /** * Common methods function. * * @param self $model * @return void */ private static function manageFields(self $model) { /** @var array $itemIngredients */ foreach ($model->ingredients as $itemIngredients) { if (isset($itemIngredients['header'])) { ToolboxHelper::assertNoInternalBoUrls('ingredients', $itemIngredients['header']); } ToolboxHelper::assertNoInternalBoUrls('ingredients', \strval($itemIngredients['value'])); } ToolboxHelper::assertNoInternalBoUrls('steps', $model->steps); ToolboxHelper::assertNoInternalBoUrls('tips', $model->tips); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); $model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true); } /** * Set the slug. * * @param self $model * * @return void */ private static function setSlug(self $model) { $model->slug = Str::slug($model->name); } /** * Set order after the last element of the list. * * @param self $model * @return void */ private static function setOrder(self $model): void { $model->order = \intval(self::query()->max('order')) + 1; } /** * Asserts using validation that the field is unique. * * @param string $nameField * @param string $valueField * @param integer|null $id * @return void */ private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id); } /** * Create and return microdata. * * @return \Spatie\SchemaOrg\Recipe */ public function toSchemaOrg(): \Spatie\SchemaOrg\Recipe { $model = $this->load(['range']); $schema = Schema::Recipe() ->name($model->seo_title) ->description($model->seo_description) ->url(route('recipe.show', $this)) ->inLanguage(config('schema-org.website.languages')[app()->currentLocale()]) ->keywords('recette') ->recipeCategory([ $model->difficulty->label(), ]) ->recipeCuisine("Française") ->recipeInstructions(strip_tags($model->tips)) ->dateCreated($model->created_at) ->dateModified($model->updated_at) ->datePublished($model->created_at) ->totalTime( Schema::duration()->setProperty("@value", $model->formatDurationIso($model->duration)) ) ->author($model->getOrganizationSchema()) ->creator($model->getOrganizationSchema()) ->image( Schema::imageObject() ->url(asset($model->picture)) ->headline($model->picture_title) ); // Steps. preg_match_all('/
  • (.*?)<\/li>/', $model->steps, $step); if (count($step[1])) { $schema->steps($step[1]); } // Ingredients. $ingredientsList = []; foreach ($model->ingredients as $ingredient) { array_push($ingredientsList, $ingredient['value']); }; $schema->ingredients($ingredientsList); // Range associated. if ($model->range->status) { $schema->subjectOf($model->range->toSchemaOrg()->toArray()); } return $schema; } /** * Get the SEO object. * * @return \Uwil\Seo\Laravel\Objects\SeoItem */ public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem { return new SeoItem( \trans($this->getTable()), $this->seo_title, $this->seo_description, $this->status, \route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this), \route(Str::singular($this->getTable()) . '.show', $this) ); } /** * To sitemap tag . * * @return \Spatie\Sitemap\Tags\Url|string|array */ public function toSitemapTag(): \Spatie\Sitemap\Tags\Url|string|array { return SitemapHelper::generateUrl('recipe.show', ['publishedRecipe' => $this], $this); } // * RELATIONSHIPS /** * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function products() { return $this->belongsToMany(Product::class, 'recipes_products'); } /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function range() { return $this->belongsTo(Range::class); } }