Kritim Yantra
Jun 25, 2025
“Ever wondered how to clean up and control the data your Laravel API returns? That’s where API Resources come in!”
When building APIs, you don’t always want to return raw database data — it can be messy, inconsistent, or reveal sensitive info. Laravel 12 gives us a powerful tool to fix this: API Resources.
In this beginner-friendly guide, you’ll learn what Laravel API Resources are, when to use them, and how to set them up — step by step.
API Resources in Laravel are like data transformers or filters.
Instead of returning full model data (with timestamps, hidden fields, etc.), you can choose exactly what to return and how.
Think of it like ordering pizza and asking for only olives, cheese, and mushrooms — no crust, no sauce.
✅ Clean, consistent API responses
✅ Hide sensitive or unnecessary fields
✅ Format data (e.g., dates) easily
✅ Add extra computed fields (like is_admin or total_price)
Let’s assume we already have a Post model and PostController.
Run this artisan command:
php artisan make:resource PostResource
This creates a file at:
app/Http/Resources/PostResource.php
Open PostResource.php and update the toArray() method:
use Illuminate\Http\Resources\Json\JsonResource;
class PostResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'created' => $this->created_at->diffForHumans(),
];
}
}
🎉 Now you're returning only the fields you want, and even customizing the created_at field into a human-readable format.
Update your PostController like this:
use App\Http\Resources\PostResource;
public function index()
{
return PostResource::collection(Post::all());
}
public function show($id)
{
$post = Post::findOrFail($id);
return new PostResource($post);
}
Boom! 💥 All your API responses are now cleaner and more professional-looking.
You can add custom fields that don’t exist in the database:
return [
'id' => $this->id,
'title' => strtoupper($this->title), // Transform title
'word_count' => str_word_count($this->content), // Add computed field
];
Let’s say your User model includes fields like password or email_verified_at.
Use a UserResource and exclude those fields easily:
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->when(auth()->user()->isAdmin(), $this->email),
];
when() helps conditionally show fields based on logic. Handy, right?
Before using resource:
{
"id": 1,
"title": "Hello World",
"content": "This is a post.",
"created_at": "2025-06-25T09:00:00.000Z",
"updated_at": "2025-06-25T09:00:00.000Z"
}
After using PostResource:
{
"id": 1,
"title": "Hello World",
"content": "This is a post.",
"created": "2 hours ago"
}
Looks cleaner, right? 😎
What if a post has an author?
You can return a nested resource like this:
use App\Http\Resources\UserResource;
return [
'id' => $this->id,
'title' => $this->title,
'author' => new UserResource($this->user),
];
This keeps your API modular and reusable — just like Lego blocks 🧱.
Now that you understand API Resources, try these challenges:
when()No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google
Kritim Yantra
Kritim Yantra
Kritim Yantra