From 545045005c837caa6bffc91b03883bdee10a3a73 Mon Sep 17 00:00:00 2001 From: A1Gard Date: Sat, 10 Aug 2024 07:15:31 +0330 Subject: [PATCH] added markup json to product and post --- .../Resources/CommentMarkupCollection.php | 29 ++++++++++++ app/Models/Post.php | 45 +++++++++++++++++++ app/Models/Product.php | 38 ++++++++++++++++ .../views/website/inc/website-head.blade.php | 6 +++ 4 files changed, 118 insertions(+) create mode 100644 app/Http/Resources/CommentMarkupCollection.php diff --git a/app/Http/Resources/CommentMarkupCollection.php b/app/Http/Resources/CommentMarkupCollection.php new file mode 100644 index 0000000..dc31da6 --- /dev/null +++ b/app/Http/Resources/CommentMarkupCollection.php @@ -0,0 +1,29 @@ + + */ + public function toArray(Request $request): array + { + return [ + '@type' => 'Review', + 'reviewBody' =>$this->body, + 'datePublished' => $this->created_at, + 'author' => + [ + '@type' => 'Person', + 'name' => $this->commentator()['name'], + ], + ]; + } +} diff --git a/app/Models/Post.php b/app/Models/Post.php index d2b7a92..aa26a78 100644 --- a/app/Models/Post.php +++ b/app/Models/Post.php @@ -133,4 +133,49 @@ class Post extends Model implements HasMedia public function webUrl(){ 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 << +{ + "@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}" +} + +RESULT; + + } } diff --git a/app/Models/Product.php b/app/Models/Product.php index e3c622f..7669302 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Http\Resources\CommentMarkupCollection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -351,4 +352,41 @@ class Product extends Model implements HasMedia } return true; } + + public function markup(){ + + + $currency = config('app.currency.code'); + $reviews = CommentMarkupCollection::collection($this->approvedComments)->toJson(); + return << +{ + "@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 +} + +RESULT; + + } } diff --git a/resources/views/website/inc/website-head.blade.php b/resources/views/website/inc/website-head.blade.php index 8e9f6a6..fd3ace9 100644 --- a/resources/views/website/inc/website-head.blade.php +++ b/resources/views/website/inc/website-head.blade.php @@ -20,6 +20,12 @@ @if(isset($breadcrumb)) {!! markUpBreadcrumbList($breadcrumb) !!} @endif + @if(isset($post)) +{!! $post->markup() !!} + @endif + @if(isset($product)) +{!! $product->markup() !!} + @endif