added markup json to product and post

master
A1Gard 1 month ago
parent 3b0017010b
commit 545045005c

@ -0,0 +1,29 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentMarkupCollection extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return [
'@type' => 'Review',
'reviewBody' =>$this->body,
'datePublished' => $this->created_at,
'author' =>
[
'@type' => 'Person',
'name' => $this->commentator()['name'],
],
];
}
}

@ -133,4 +133,49 @@ class Post extends Model implements HasMedia
public function webUrl(){ public function webUrl(){
return route('client.post',$this->slug); return route('client.post',$this->slug);
} }
public function markup(){
$type = 'BlogPosting';
if (strpos(strtolower(implode(',',$this->groups()->pluck('name')->toArray())),__('article')) !== false){
$type = 'Article';
}
if (strpos(strtolower(implode(',',$this->groups()->pluck('name')->toArray())),__('news')) !== false){
$type = 'NewsArticle';
}
$app = config('app.name');
$logo = asset('upload/images/logo.png');
$author = $this->author->name??$app;
return <<<RESULT
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "$type",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "{$this->webUrl()}"
},
"headline": "{$this->title}",
"description": "{$this->subtitle}",
"image": "{$this->imgUrl()}",
"author": {
"@type": "Person",
"name": "$author"
},
"publisher": {
"@type": "Organization",
"name": "$app",
"logo": {
"@type": "ImageObject",
"url": "$logo"
}
},
"datePublished": "{$this->created_at}",
"dateModified": "{$this->updated_at}"
}
</script>
RESULT;
}
} }

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Http\Resources\CommentMarkupCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -351,4 +352,41 @@ class Product extends Model implements HasMedia
} }
return true; return true;
} }
public function markup(){
$currency = config('app.currency.code');
$reviews = CommentMarkupCollection::collection($this->approvedComments)->toJson();
return <<<RESULT
<script type="application/ld+json">
{
"@context": "https://schema.org/",
"@type": "Product",
"name": "name",
"image": "{$this->name}",
"description": "{$this->excerpt}",
"brand": {
"@type": "Brand",
"name": "{$this->category->name}"
},
"sku": "{$this->sku}",
"offers": {
"@type": "Offer",
"url": "{$this->webUrl()}",
"priceCurrency": "$currency",
"price": "{{$this->price}}"
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "{$this->average_rating}",
"ratingCount": "{$this->rating_count}",
"reviewCount": "{$this->approvedComments()->count()}"
},
"review": $reviews
}
</script>
RESULT;
}
} }

@ -20,6 +20,12 @@
@if(isset($breadcrumb)) @if(isset($breadcrumb))
{!! markUpBreadcrumbList($breadcrumb) !!} {!! markUpBreadcrumbList($breadcrumb) !!}
@endif @endif
@if(isset($post))
{!! $post->markup() !!}
@endif
@if(isset($product))
{!! $product->markup() !!}
@endif
</head> </head>
<body @yield('body-attr')> <body @yield('body-attr')>

Loading…
Cancel
Save