0
0
Laravelframework~10 mins

API resource classes in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - API resource classes
Create Resource Class
Define toArray()
Controller returns Resource
Resource transforms Model
JSON Response Sent
This flow shows how Laravel API resource classes transform data models into JSON responses step-by-step.
Execution Sample
Laravel
class UserResource extends JsonResource {
  public function toArray($request) {
    return [
      'id' => $this->id,
      'name' => $this->name
    ];
  }
}
Defines a resource class that converts a User model into a simple JSON with id and name.
Execution Table
StepActionInputOutputNotes
1Create UserResource instanceUser model with id=5, name='Anna'UserResource wrapping User modelResource object created with model data
2Call toArray()UserResource instance{"id": 5, "name": "Anna"}Resource converts model to array
3Return Resource from ControllerUserResource instanceJSON response with {"id":5,"name":"Anna"}Laravel sends JSON response
4Client receives responseJSON responseParsed JSON objectClient can use data
💡 Response sent and client received JSON, process ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
User model{"id":5, "name":"Anna"}{"id":5, "name":"Anna"}{"id":5, "name":"Anna"}{"id":5, "name":"Anna"}{"id":5, "name":"Anna"}
UserResourcenullUserResource wrapping User modelUserResource wrapping User modelUserResource wrapping User modelnull (converted to JSON)
Array outputnullnull{"id":5,"name":"Anna"}{"id":5,"name":"Anna"}null (sent as JSON)
Key Moments - 3 Insights
Why do we use the toArray() method inside the resource class?
toArray() defines exactly how the model data is converted into an array for JSON output, as shown in step 2 of the execution_table.
Does the resource class modify the original model data?
No, the resource wraps the model and creates a new array representation without changing the original model, as seen in variable_tracker where the User model stays unchanged.
What happens if you return the model directly from the controller instead of the resource?
Returning the model directly sends all attributes without transformation or filtering, unlike the resource which controls output format (step 3 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of the toArray() method at step 2?
A{"id": 5, "name": "Anna"}
BUserResource instance
CJSON string
DUser model object
💡 Hint
Check the Output column at step 2 in the execution_table.
At which step does Laravel send the JSON response to the client?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where the Output is described as JSON response in the execution_table.
If the toArray() method included an extra field 'email', how would the output at step 2 change?
AIt would remove 'name' from the output
BIt would cause an error
CIt would include 'email' in the array output
DIt would not change the output
💡 Hint
Refer to how toArray() defines the output array in the execution_table step 2.
Concept Snapshot
Laravel API Resource Classes:
- Create a resource class extending JsonResource
- Define toArray() to specify output fields
- Return resource from controller
- Laravel converts resource to JSON response
- Controls API output format cleanly
Full Transcript
Laravel API resource classes help convert data models into JSON responses for APIs. First, you create a resource class extending JsonResource. Inside, you define the toArray() method to specify which model fields to include in the output. When a controller returns this resource, Laravel calls toArray(), converts the result to JSON, and sends it as the API response. This process keeps your API output clean and consistent without changing the original model data.