|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Http\Resources\Api\V1\Article; |
| 6 | + |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\Http\Resources\Json\JsonResource; |
| 9 | + |
| 10 | +/** |
| 11 | + * @mixin \App\Models\Article |
| 12 | + */ |
| 13 | +class ArticleResource extends JsonResource |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Transform the resource into an array. |
| 17 | + * |
| 18 | + * @return array<string, mixed> |
| 19 | + */ |
| 20 | + public function toArray(Request $request): array |
| 21 | + { |
| 22 | + return [ |
| 23 | + 'id' => $this->id, |
| 24 | + 'slug' => $this->slug, |
| 25 | + 'title' => $this->title, |
| 26 | + 'subtitle' => $this->subtitle, |
| 27 | + 'excerpt' => $this->excerpt, |
| 28 | + 'content_html' => $this->content_html, |
| 29 | + 'content_markdown' => $this->content_markdown, |
| 30 | + 'featured_image' => $this->featured_image, |
| 31 | + 'status' => $this->status, |
| 32 | + 'published_at' => $this->published_at?->toISOString(), |
| 33 | + 'meta_title' => $this->meta_title, |
| 34 | + 'meta_description' => $this->meta_description, |
| 35 | + 'created_at' => $this->created_at?->toISOString(), |
| 36 | + 'updated_at' => $this->updated_at?->toISOString(), |
| 37 | + |
| 38 | + // Relationships |
| 39 | + 'author' => $this->whenLoaded('author', function () { |
| 40 | + return $this->author ? [ |
| 41 | + 'id' => $this->author->id, |
| 42 | + 'name' => $this->author->name, |
| 43 | + 'email' => $this->author->email, |
| 44 | + 'avatar_url' => $this->author->avatar_url, |
| 45 | + 'bio' => $this->author->bio, |
| 46 | + 'twitter' => $this->author->twitter, |
| 47 | + 'facebook' => $this->author->facebook, |
| 48 | + 'linkedin' => $this->author->linkedin, |
| 49 | + 'github' => $this->author->github, |
| 50 | + 'website' => $this->author->website, |
| 51 | + ] : null; |
| 52 | + }), |
| 53 | + |
| 54 | + 'categories' => $this->whenLoaded('categories', function () { |
| 55 | + /** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\Category> $categories */ |
| 56 | + $categories = $this->categories; |
| 57 | + |
| 58 | + return $categories->map(function ($category) { |
| 59 | + return [ |
| 60 | + 'id' => $category->id, |
| 61 | + 'name' => $category->name, |
| 62 | + 'slug' => $category->slug, |
| 63 | + ]; |
| 64 | + })->values()->all(); |
| 65 | + }), |
| 66 | + |
| 67 | + 'tags' => $this->whenLoaded('tags', function () { |
| 68 | + /** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\Tag> $tags */ |
| 69 | + $tags = $this->tags; |
| 70 | + |
| 71 | + return $tags->map(function ($tag) { |
| 72 | + return [ |
| 73 | + 'id' => $tag->id, |
| 74 | + 'name' => $tag->name, |
| 75 | + 'slug' => $tag->slug, |
| 76 | + ]; |
| 77 | + })->values()->all(); |
| 78 | + }), |
| 79 | + |
| 80 | + 'authors' => $this->whenLoaded('authors', function () { |
| 81 | + /** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $authors */ |
| 82 | + $authors = $this->authors; |
| 83 | + |
| 84 | + return $authors->map(function ($author) { |
| 85 | + /** @var \Illuminate\Database\Eloquent\Relations\Pivot|null $pivot */ |
| 86 | + $pivot = $author->getAttribute('pivot'); |
| 87 | + |
| 88 | + return [ |
| 89 | + 'id' => $author->id, |
| 90 | + 'name' => $author->name, |
| 91 | + 'email' => $author->email, |
| 92 | + 'avatar_url' => $author->avatar_url, |
| 93 | + 'bio' => $author->bio, |
| 94 | + 'twitter' => $author->twitter, |
| 95 | + 'facebook' => $author->facebook, |
| 96 | + 'linkedin' => $author->linkedin, |
| 97 | + 'github' => $author->github, |
| 98 | + 'website' => $author->website, |
| 99 | + 'role' => $pivot?->getAttribute('role'), |
| 100 | + ]; |
| 101 | + })->values()->all(); |
| 102 | + }), |
| 103 | + |
| 104 | + 'comments_count' => $this->whenCounted('comments'), |
| 105 | + ]; |
| 106 | + } |
| 107 | +} |
0 commit comments