Skip to content

Commit e596ab0

Browse files
author
ahmadhuss
committed
refactor: consume CategoryResource for all records
1 parent 132769b commit e596ab0

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

app/Http/Controllers/Api/CategoryController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class CategoryController extends Controller
1313
public function index(){
1414
// return Category::orderBy('id')->get(); // Laravel will return as JSON format.
1515
// We only want id, name filed in the response.
16-
return Category::select('id','name')->get();
16+
// $categories = Category::select('id','name')->get(); This is efficient approach.
17+
// But we are already defined our toArray response
18+
$categories = Category::all();
19+
return CategoryResource::collection($categories); // Now we are returning resource collection
1720
}
1821

1922
// Route model binding

0 commit comments

Comments
 (0)