Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| CategoryController | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| index | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Api\V1; |
| 4 | |
| 5 | use App\Http\Controllers\Api\ApiController; |
| 6 | use App\Http\Resources\V1\CategoryResource; |
| 7 | use App\Models\Category; |
| 8 | use Illuminate\Http\JsonResponse; |
| 9 | |
| 10 | /** GET /api/v1/categories — top-level categories with their immediate children. */ |
| 11 | class CategoryController extends ApiController |
| 12 | { |
| 13 | public function index(): JsonResponse |
| 14 | { |
| 15 | $categories = Category::query() |
| 16 | ->where('isActive', true) |
| 17 | ->whereNull('parent_id') |
| 18 | ->with(['children' => fn ($q) => $q->where('isActive', true)]) |
| 19 | ->orderBy('name') |
| 20 | ->get(); |
| 21 | |
| 22 | return $this->respond(CategoryResource::collection($categories)); |
| 23 | } |
| 24 | } |