taktik - laravel
This commit is contained in:
89
app/Http/Resources/PostResource.php
Normal file
89
app/Http/Resources/PostResource.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @mixin Post
|
||||
* @OA\Schema(
|
||||
* schema="PostResource",
|
||||
* type="object",
|
||||
* title="Post Resource",
|
||||
* description="Resource representing a single post",
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="integer",
|
||||
* description="ID of the post",
|
||||
* example=1
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="title",
|
||||
* type="string",
|
||||
* description="Title of the post",
|
||||
* example="Sample Post Title"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="content",
|
||||
* type="string",
|
||||
* description="Content of the post",
|
||||
* example="This is a sample post content."
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="category_id",
|
||||
* type="integer",
|
||||
* description="Category ID the post belongs to",
|
||||
* example=1
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="category",
|
||||
* description="Category object",
|
||||
* ref="#/components/schemas/CategoryResource",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="tags",
|
||||
* ref="#/components/schemas/TagCollection",
|
||||
* description="Collection of tags related to the post"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="created_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the post was created",
|
||||
* example="2023-12-10T15:24:00Z"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="updated_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the post was last updated",
|
||||
* example="2023-12-10T15:26:00Z"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
class PostResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource collection into an array.
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'category_id' => $this->category_id,
|
||||
'category' => new CategoryResource($this->whenLoaded('category')),
|
||||
'tags' => new TagCollection($this->whenLoaded('tags')),
|
||||
'created_at' => $this->created_at->toDateTimeString(),
|
||||
'updated_at' => $this->updated_at->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user