aiblog/app/Http/Resources/PostResource.php
2024-11-17 11:30:01 +08:00

27 lines
No EOL
797 B
PHP

<?php
declare(strict_types=1);
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class PostResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'summary' => $this->summary,
'content' => $this->content,
'views' => $this->views,
'author' => new UserResource($this->whenLoaded('author')),
'category' => new CategoryResource($this->whenLoaded('category')),
'comments' => CommentResource::collection($this->whenLoaded('comments')),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}