Laravel 5.4
Awesome Laravel
- Awesome Laravel (Chirag Gude)
 Prologue
- Release Notes
 - Upgrade Guide
 Getting Started
- Installation
 - Configuration
 - Directory Structure
 - Laravel Homestead
 - valet
 Architecture Concepts
- Request Lifecycle
 - Service Container
 - Service Providers
 - Facades
 The Basics
- Routing
 - Errors & Logging
 - Middleware
 - CSRF Protection
 - Controllers
 - HTTP Requests
 - HTTP Responses
 - Views
 - HTTP Session
 - Validation
 Frontend
- Blade Templates
 - Localization
 - JavaScript & CSS Scaffolding
 - Compiling Assets (Laravel Mix)
 Security
- Authentication
 - API Authentication (Passport)
 - Authorization
 - Encryption
 - Hashing
 - Resetting Passwords
 Digging Deeper
- Artisan Console
 - Queues
 - Package Development
 - Task Scheduling
 - Broadcasting
 - Cache
 - Collections
 - Events
 - File Storage
 - helpers
 - Notifications
 Database
- Database Getting Started
 - Database Query Builder
 - Database Pagination
 - Database Migrations
 - Database Seeding
 - Redis
 Eloquent ORM
- Eloquent Getting Started
 - Eloquent Relationships
 - Eloquent Collections
 - Eloquent Mutators
 - Eloquent Serialization
 Testing
- Testing Getting Started
 - HTTP Tests
 - Browser Tests (Laravel Dusk)
 - Database Testing
 - Mocking
 - redirect
 Official Packages
- Laravel Cashier
 - Envoy Task Runner
 - Laravel Scout
 
Eloquent Serialization
Introduction
When building JSON APIs, you will often need to convert your models and relationships to arrays or JSON. Eloquent includes convenient methods for making these conversions, as well as controlling which attributes are included in your serializations.
Serializing Models & Collections
Serializing To Arrays
To convert a model and its loaded relationships to an array, you should use the toArray method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays:
You may also convert entire collections of models to arrays:
Serializing To JSON
To convert a model to JSON, you should use the toJson method. Like toArray, the toJson method is recursive, so all attributes and relations will be converted to JSON:
Alternatively, you may cast a model or collection to a string, which will automatically call the toJson method on the model or collection:
Since models and collections are converted to JSON when cast to a string, you can return Eloquent objects directly from your application’s routes or controllers:
Hiding Attributes From JSON
Sometimes you may wish to limit the attributes, such as passwords, that are included in your model’s array or JSON representation. To do so, add a $hidden property to your model:
{note} When hiding relationships, use the relationship’s method name, not its dynamic property name.
Alternatively, you may use the visible property to define a white-list of attributes that should be included in your model’s array and JSON representation. All other attributes will be hidden when the model is converted to an array or JSON:
Temporarily Modifying Attribute Visibility
If you would like to make some typically hidden attributes visible on a given model instance, you may use the makeVisible method. The makeVisible method returns the model instance for convenient method chaining:
Likewise, if you would like to make some typically visible attributes hidden on a given model instance, you may use the makeHidden method.
Appending Values To JSON
Occasionally, when casting models to an array or JSON, you may wish to add attributes that do not have a corresponding column in your database. To do so, first define an accessor for the value:
After creating the accessor, add the attribute name to the appends property on the model. Note that attribute names are typically referenced in “snake case”, even though the accessor is defined using “camel case”:
Once the attribute has been added to the appends list, it will be included in both the model’s array and JSON representations. Attributes in the appends array will also respect the visible and hidden settings configured on the model.