fix: 🐛 resolve model relations in schemaorg methods

This commit is contained in:
Alexis Gousseau 2025-06-11 08:16:40 +00:00 • committed by Vincent BRIDIER
parent 04dab9359a
commit c295259c3f

View file

@ -280,44 +280,46 @@ class Recipe extends Model implements SeoInterface, Sitemapable, SchemaOrgInterf
*/
public function toSchemaOrg(bool $asReference = false): \Spatie\SchemaOrg\Recipe
{
$model = $this->load(['consumptionMoment']);
// Main.
$schema = Schema::Recipe()
->name($this->title)
->description($this->seo_description)
->url(route('fo.recipes.show', $this))
->name($model->title)
->description($model->seo_description)
->url(route('fo.recipes.show', $model))
->recipeCategory([
$this->consumptionMoment->title,
$this->difficulty->label(),
$model->consumptionMoment->title,
$model->difficulty->label(),
])
->recipeInstructions($this->tips)
->dateCreated($this->created_at)
->dateModified($this->updated_at)
->datePublished($this->created_at)
->recipeInstructions($model->tips)
->dateCreated($model->created_at)
->dateModified($model->updated_at)
->datePublished($model->created_at)
->cookTime(
Schema::duration()->setProperty("@value", $this->formatDurationIso($this->cooking_time))
Schema::duration()->setProperty("@value", $model->formatDurationIso($model->cooking_time))
)
->prepTime(
Schema::duration()->setProperty("@value", $this->formatDurationIso($this->preparation_time))
Schema::duration()->setProperty("@value", $model->formatDurationIso($model->preparation_time))
)
->totalTime(
Schema::duration()->setProperty("@value", $this->formatDurationIso(
$this->cooking_time + $this->preparation_time
Schema::duration()->setProperty("@value", $model->formatDurationIso(
$model->cooking_time + $model->preparation_time
))
)
->author($this->getOrganizationSchema())
->creator($this->getOrganizationSchema())
->author($model->getOrganizationSchema())
->creator($model->getOrganizationSchema())
->image(
Schema::imageObject()
->url(asset($this->picture))
->headline($this->picture_title)
->url(asset($model->picture))
->headline($model->picture_title)
);
// Steps associated.
preg_match_all('/<li>(.*?)<\/li>/', $this->preparation, $step);
preg_match_all('/<li>(.*?)<\/li>/', $model->preparation, $step);
$schema->steps($step[1]);
// Ingredients associated.
$ingredientsList = collect($this->ingredients_list)
$ingredientsList = collect($model->ingredients_list)
->map(function ($item) {
// @phpstan-ignore ternary.elseUnreachable
return trim(($item['label_unit'] !== null ? $item['label_unit'] : '') . ' ' . $item['label_value']);
@ -326,21 +328,21 @@ class Recipe extends Model implements SeoInterface, Sitemapable, SchemaOrgInterf
// Products associated.
if (!$asReference) {
$linkedProducts = $this->products->map(function (Product $product) {
$linkedProducts = $model->products->map(function (Product $product) {
return $product->toSchemaOrg(true);
})->toArray();
$schema->subjectOf($linkedProducts);
}
// Video associated.
if ($this->video_id) {
$url = (IsValidVimeoId::validates($this->video_id))
? "https://player.vimeo.com/video/$this->video_id"
: "https://www.youtube.com/embed/$this->video_id";
if ($model->video_id) {
$url = (IsValidVimeoId::validates($model->video_id))
? "https://player.vimeo.com/video/$model->video_id"
: "https://www.youtube.com/embed/$model->video_id";
$schema->associatedMedia(
Schema::mediaObject()
->name($this->video->title)
->productionCompany($this->getOrganizationSchema())
->name($model->video->title)
->productionCompany($model->getOrganizationSchema())
->embedUrl($url)
);
}