33 lines
919 B
PHP
33 lines
919 B
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class CategoryResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->name,
|
|
'slug' => $this->slug,
|
|
'description' => $this->description,
|
|
'short_description' => $this->short_description,
|
|
'image' => $this->image,
|
|
'posts_count' => $this->whenLoaded('posts', function () {
|
|
return $this->posts->count();
|
|
}),
|
|
'created_at' => Carbon::parse($this->created_at)->format('M d Y'),
|
|
'updated_at' => Carbon::parse($this->updated_at)->format('M d Y'),
|
|
];
|
|
}
|
|
}
|