Back to Blog
Backend•7 min read
How to Build a REST API in Laravel With Resource Collections
Priya Sharma
Full Stack Dev
Mar 10, 2026
Returning raw Eloquent models often exposes data you want to hide (like passwords or internal timestamps). API Resources allow you to transform your data before sending it to the client.
Creating a Resource
bash
php artisan make:resource UserResourcephp
<?php
namespace AppHttpResources;
use IlluminateHttpResourcesJsonJsonResource;
class UserResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'full_name' => $this->first_name . ' ' . $this->last_name,
'email' => $this->email,
'created_at' => $this->created_at->format('d-m-Y'),
];
}
}Share this article:
