| 123456789101112131415161718192021222324252627282930313233 |
- <?php
- namespace App\Http\Resources;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
- use Illuminate\Http\Resources\Json\JsonResource;
- class GroupResource extends JsonResource
- {
- public function toArray(Request $request): array
- {
- return [
- 'id' => $this->id,
- 'name' => $this->name,
- 'status' => $this->status,
- 'active_units' => $this->whenLoaded('units', fn() => $this->units->count()),
- 'unit_ids' => $this->whenLoaded('units', fn() => $this->units->pluck('id')),
- 'units' => $this->whenLoaded('units', fn() => $this->units->map(fn($unit) => [
- 'id' => $unit->id,
- 'fantasy_name' => $unit->fantasy_name,
- ])),
- 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d'),
- 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d'),
- ];
- }
- public static function collection($resource): AnonymousResourceCollection
- {
- return parent::collection($resource);
- }
- }
|