How to Use Transformer in Laravel: Simple Guide with Examples
In Laravel, you use
transformers to format and shape API response data consistently by creating a class that transforms your models or data arrays. Typically, you define a transformer class with a transform method and use it in your controllers or resources to return clean JSON responses.Syntax
A transformer in Laravel is a class with a transform method that takes a model or data and returns an array with the desired structure. You then use this transformer to convert your data before sending it as a response.
Key parts:
transform($item): Method to define how to convert the input data.- Return an array with only the fields you want to expose.
- Use the transformer in your controller or resource to format output.
php
class UserTransformer { public function transform($user) { return [ 'id' => $user->id, 'name' => $user->name, 'email' => $user->email, ]; } }
Example
This example shows a simple UserTransformer class and how to use it in a controller to return a transformed JSON response.
php
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\JsonResponse; class UserController extends Controller { public function show($id): JsonResponse { $user = User::findOrFail($id); $transformer = new UserTransformer(); $transformedUser = $transformer->transform($user); return response()->json($transformedUser); } } class UserTransformer { public function transform($user) { return [ 'id' => $user->id, 'name' => $user->name, 'email' => $user->email, ]; } }
Output
{"id":1,"name":"John Doe","email":"john@example.com"}
Common Pitfalls
Common mistakes when using transformers in Laravel include:
- Returning the entire model directly instead of a transformed array, which can expose sensitive data.
- Not handling null or missing data inside the
transformmethod, causing errors. - Using transformers inconsistently, leading to mixed response formats.
Always explicitly define the fields you want to expose and handle edge cases gracefully.
php
<?php // Wrong: returning model directly return response()->json($user); // Right: using transformer $transformer = new UserTransformer(); return response()->json($transformer->transform($user));
Quick Reference
| Step | Description |
|---|---|
| Create Transformer Class | Define a class with a transform() method returning an array. |
| Use Transformer | Instantiate and call transform() on your data in controller. |
| Return JSON | Send transformed data as JSON response. |
| Handle Nulls | Check for null or missing fields inside transform(). |
| Expose Only Needed Fields | Return only safe and necessary data. |
Key Takeaways
Create a transformer class with a transform() method to shape your data.
Always return arrays with only the fields you want to expose in API responses.
Use transformers in controllers to keep response formatting consistent.
Handle null or missing data inside the transform method to avoid errors.
Avoid returning raw models directly to protect sensitive information.