Why this lesson matters
Understand json responses 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 JSON Responses, the outcome to verify is: A resource keeps the JSON contract independent from the model internals.
- In JSON Responses, 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.
- JSON Responses practice target: Create one JSON endpoint with validation and a Resource, then test success and validation-error responses.
Practical walkthrough
In the JSON Responses 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
JSON Responses 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
