first commit
This commit is contained in:
38
laravel/app/Http/Resources/ArticleDetailResource.php
Normal file
38
laravel/app/Http/Resources/ArticleDetailResource.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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(),
|
||||
];
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
32
laravel/app/Http/Resources/ArticleResource.php
Normal file
32
laravel/app/Http/Resources/ArticleResource.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\Article;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @mixin Article
|
||||
*/
|
||||
final class ArticleResource 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_short' => $this->content ? substr($this->content, 0, 50) : '',
|
||||
'created_at' => $this->created_at?->toDateString() ?? '',
|
||||
'comments_count' => $this->comments_count ?? 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
31
laravel/app/Http/Resources/CommentResource.php
Normal file
31
laravel/app/Http/Resources/CommentResource.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\Comment;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @mixin Comment
|
||||
*/
|
||||
final class CommentResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'author_name' => $this->author_name ?? '',
|
||||
'content' => $this->content ?? '',
|
||||
'created_at' => $this->created_at?->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user