Why this lesson matters

Understand api routes and resources and apply it in a small Laravel feature. A Laravel API is a public contract: routes define the surface, validation protects input, resources shape output, and status/error behavior lets clients integrate predictably.

How to reason about it

  • For API Routes and Resources, the outcome to verify is: A resource keeps the JSON contract independent from the model internals.
  • In API Routes and Resources, keep this failure controlled: Returning raw model attributes or exception messages creates a brittle API and can leak fields that were never meant to be public.
  • API Routes and Resources practice target: Create one JSON endpoint with validation and a Resource, then test success and validation-error responses.
Diagram

API route to resource

Practical walkthrough

In the API Routes and Resources walkthrough: A resource keeps the JSON contract independent from the model internals.

app/Http/Controllers/Api/UserController.phpphp
public function index()
{
    return UserResource::collection(
        User::query()->paginate(20)
    );
}

Practice it yourself

Practice

API Routes and Resources exercise

Create one JSON endpoint with validation and a Resource, then test success and validation-error responses.

  • Record the expected result before execution
  • Test one valid path and one lesson-specific failure path
  • Explain in two lines which boundary owns the decision

Summary