#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Using API Resources and Resource Collections

Returning an Eloquent model directly from a controller exposes every column, including ones you may not want clients to see. Laravel's API Resources solve this by giving a dedicated transformation layer between models and JSON output.

Generating a Resource

php artisan make:resource PostResource

This creates a class in app/Http/Resources with a toArray() method where the exact output shape is defined.

public function toArray($request)
{
    return [
        'id' => $this->id,
        'title' => $this->title,
        'excerpt' => Str::limit($this->body, 100),
        'published_at' => $this->created_at->toDateString(),
    ];
}

Resource Collections

When returning many records, a Resource Collection wraps each item with the resource's transformation and adds a data key automatically.

return PostResource::collection(Post::all());

Why This Matters

With clean, predictable output in place, attention usually turns to controlling who is allowed to call these endpoints at all.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course