cslant /
blog-api-package
| 1 | <?php |
||
| 2 | |||
| 3 | namespace CSlant\Blog\Api\Http\Resources\Post; |
||
| 4 | |||
| 5 | use CSlant\Blog\Api\Http\Resources\Author\AuthorResource; |
||
| 6 | use CSlant\Blog\Api\Http\Resources\Category\CategoryResource; |
||
| 7 | use CSlant\Blog\Api\Http\Resources\Tag\TagResource; |
||
| 8 | use CSlant\Blog\Core\Facades\Base\Media\RvMedia; |
||
| 9 | use CSlant\Blog\Core\Models\Post; |
||
| 10 | use CSlant\Blog\Core\Models\Slug; |
||
| 11 | use Illuminate\Http\Request; |
||
| 12 | use Illuminate\Http\Resources\Json\JsonResource; |
||
| 13 | use Illuminate\Support\Facades\Auth; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @mixin Post |
||
| 17 | */ |
||
| 18 | class ListPostResource extends JsonResource |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @param Request $request |
||
| 22 | * |
||
| 23 | * @return array<string, mixed> |
||
| 24 | */ |
||
| 25 | public function toArray($request): array |
||
| 26 | { |
||
| 27 | $userId = Auth::user() ? Auth::user()->id : 0; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | |||
| 29 | /** @var Post $this */ |
||
| 30 | return [ |
||
| 31 | 'id' => $this->id, |
||
| 32 | 'name' => $this->name, |
||
| 33 | 'slug' => $this->slug instanceof Slug ? $this->slug->key : $this->slug, |
||
|
0 ignored issues
–
show
|
|||
| 34 | 'description' => $this->description, |
||
| 35 | 'image' => $this->image ? RvMedia::url($this->image) : null, |
||
| 36 | 'categories' => CategoryResource::collection($this->categories), |
||
| 37 | 'tags' => TagResource::collection($this->tags), |
||
| 38 | 'author' => AuthorResource::make($this->author), |
||
| 39 | 'likes_count' => $this->relationLoaded('likes') ? $this->likesCountDigital() : 0, |
||
| 40 | 'comments_count' => $this->relationLoaded('comments') ? $this->comments()->count() : 0, |
||
| 41 | 'is_liked' => $this->relationLoaded('likes') && $this->isLikedBy($userId), |
||
| 42 | 'is_commented' => $this->relationLoaded('comments') && $this->isCommentBy($userId), |
||
| 43 | 'created_at' => $this->created_at, |
||
| 44 | 'updated_at' => $this->updated_at, |
||
| 45 | ]; |
||
| 46 | } |
||
| 47 | } |
||
| 48 |