Challenge 1: Write a Basic Resource — Possible Solution ==================================================================== # php artisan make:resource AuthorResource // app/Http/Resources/AuthorResource.php namespace App\Http\Resources; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class AuthorResource extends JsonResource { public function toArray(Request $request): array { return [ 'id' => $this->id, 'name' => $this->name, 'bio' => $this->bio, ]; } } // app/Http/Controllers/AuthorController.php use App\Http\Resources\AuthorResource; use App\Models\Author; public function index() { return AuthorResource::collection(Author::all()); } # Example response for GET /authors: # { # "data": [ # { "id": 1, "name": "Ursula K. Le Guin", "bio": "..." }, # { "id": 2, "name": "Octavia E. Butler", "bio": "..." } # ] # } WHY THIS WORKS -------------- - toArray() explicitly lists exactly which fields to include (id, name, bio) — like ModelForm.Meta.fields and DRF's serializer fields, this is an intentional allowlist: any other column on the Author model (like an internal timestamp not meant for the API, or a future sensitive field) simply isn't exposed unless it's added here deliberately. - $this inside a Resource's toArray() refers to the underlying model instance being wrapped — $this->id and $this->name access the Author model's actual attributes directly, the same as accessing them on the model itself. - AuthorResource::collection(Author::all()) wraps an entire Eloquent Collection, automatically producing a {"data": [...]} envelope with one transformed object per Author — the plural collection() method handles the iteration, so toArray() only needs to describe the shape of ONE item.