Files
test_polis/laravel/app/Http/Resources/ArticleDetailResource.php
2026-02-04 23:23:42 +07:00

39 lines
1002 B
PHP

<?php
declare(strict_types=1);
namespace App\Http\Resources;
use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin Article
*/
final class ArticleDetailResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title ?? '',
'content' => $this->content ?? '',
'created_at' => $this->created_at?->toDateString() ?? '',
'comments' => $this->comments->map(function ($comment) {
return [
'id' => $comment->id,
'author_name' => $comment->author_name,
'content' => $comment->content,
'created_at' => $comment->created_at?->toDateTimeString(),
];
}),
];
}
}