Laravel theorytheory 0/50 · 0%
API · medium

29. API Resources

Transforming models into JSON.

API Resources transform Eloquent models and collections into JSON structures, decoupling your database schema from your API's public shape. Created with php artisan make:resource, a resource class's toArray() method defines the output format, and ResourceCollection handles collections with pagination metadata. Resources can conditionally include attributes with whenLoaded() and mergeWhen().

Check your understanding

  1. 1. What method defines a resource's JSON output?

  2. 2. Which method conditionally includes a relationship only if it was eager loaded?