In this lesson, we will take on two tasks:
- Implementing Laravel Eloquent API Resource
- Implementing CRUD operations for the
Categorymodel
At the end of this lesson, we will have a fully working CRUD for the Category model with transformed API responses:

Creating First Resource
Let's start by creating our first resource using the artisan command:
php artisan make:resource CategoryResourceThis will create a scaffold for our resource:
app/Http/Resources/CategoryResource.php
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 parent::toArray($request); }}Here, we should focus on the toArray method. This is where we can transform our model data into the desired format. So let's add our transformation logic:
app/Http/Resources/CategoryResource.php
use App\Models\Category;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 parent::toArray($request); return [ 'id' => $this->id, 'name' => $this->name, ]; }}We have also added a mixing annotation to the class. This will help IDE to understand that this resource is related to the Category model and provide better code completion.
Using Resource in Controller
The last thing we must do is use this resource in our Controller. Let's update our CategoryController:
app/Http/Controllers/CategoryController.php
use App\Http\Resources\CategoryResource;// ... public function index(){ return CategoryResource::collection(Category::all());}Now, if we run our Postman request, we should see the transformed response:

If we need to add more fields to the response, we can easily do that by updating the CategoryResource class:
app/Http/Resources/CategoryResource.php
return [ 'id' => $this->id, 'name' => $this->name, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at,];Now our response will include created_at and updated_at fields as well:

This is a great way to keep our API responses consistent and clean.
Note: Remember to remove the created_at and updated_at fields, which are an example of how to add more fields to the response.
Another thing you might have noticed is that we have a difference in response structure.
- Without Resource, we get an array like this
[{...}, {...}, ...] - With Resource, we get an array like this
{"data": [{...}, {...}, ...]}
Great course so far, just a small thing I noticed, references to 'app/Http/Controllers/CategoryController' should be -> 'app/Http/Controllers/Api/CategoryController'
I just checked the code, and it should not have the
/Apiprefix since we are not using the prefix.The reason for this is - we are building API only application. So there's no reason to add that prefix to namespace, since in our tutorial - we don't expect any non-api routes.
That makes sense, but in the first lesson this is the code ran that created the controller:
Maybe I'm missing something but that makes this lessons references to
app/Http/Controllers/CategoryControllerseem wrong. Either way, a minor detail, really enjoyed the course.Whoops! I do need to adjust the command. Thanks for that!
And yes, it is minor, but does create confusion... Sorry!
No worries :) I figured I'd share because I know personally I'd want to fix it...cheers!
For sure! We sometimes miss things, so fixing them is a priority for us :) (also fixed the issue!)
It's easy to miss things like that for sure, I had to go back and check, command looks good...but the first lesson still references
app/Http/Controllers/Api/CategoryController.phpin a few places...sorry, the QA part of my brain can't help it :)Fixed! Missed that file :(
Also, lovely QA part you have there!
Looks good! and thank you :) I think we've done enough for today, time to clockout