-
app/Http/Resources/OfficeResource.php
Open in GitHubuse Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Arr; class OfficeResource extends JsonResource { public function toArray($request) { return [ 'user' => UserResource::make($this->whenLoaded('user')), 'images' => ImageResource::collection($this->whenLoaded('images')), 'tags' => TagResource::collection($this->whenLoaded('tags')), 'featured_image' => ImageResource::make($this->whenLoaded('featuredImage')), 'reservations_count' => $this->resource->reservations_count ?? 0, $this->merge(Arr::except(parent::toArray($request), [ 'user_id', 'created_at', 'updated_at', 'deleted_at' ])) ]; } }
-
app/Http/Controllers/OfficeController.php
Open in GitHubuse App\Http\Resources\OfficeResource; use App\Models\Office; use App\Models\Reservation; use Illuminate\Http\Resources\Json\JsonResource; class OfficeController extends Controller { // public function show(Office $office): JsonResource { $office->loadCount(['reservations' => fn($builder) => $builder->where('status', Reservation::STATUS_ACTIVE)]) ->load(['images', 'tags', 'user']); return OfficeResource::make($office); } // }