Eloquent:serialization
- Introduction
- Basic Usage
- Hiding Attributes from JSON
- Appending Values to JSON
Introduction
When the building JSON APIs, you'll 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 is included in Your serializations.
Basic Usage converting A Model to an Array
To-Convert a model and its loaded relationships to an array, the toArray
method. This method was recursive, so all attributes and all relations (including the relations of relations) would be converted to Arrays
$user = App\User::with(‘roles‘)->first();return $user->toArray();
Also convert collections to arrays:
$users = App\User::all();return $users->toArray();
Converting A Model to JSON
To-Convert a model to JSON, if you would use the toJson
method. Like toArray
, the toJson
method was recursive, so all attributes and relations would be a converted to JSON:
$user = App\User::find(1);return $user->toJson();
Alternatively, you could cast a model or collection to a string, which would automatically call the toJson
method:
$user = App\User::find(1);return (string) $user;
Since models and collections is converted to JSON when cast to a string, can return eloquent objects directly from yo ur application ' s routes or controllers:
Route::get(‘users‘, function () { return App\User::all();});
Hiding Attributes from JSON
Sometimes wish to limit the attributes, such as passwords, that is included in your model ' s array or JSON Represe Ntation. To does so, add a property $hidden
definition to your model:
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{ /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [‘password‘];}
Note: When hiding relationships, use the relationship's method name, not its dynamic property name.
Alternatively, you could use the property to visible
define a white-list of attributes that should being included in your model ' s Array and JSON representation:
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{ /** * The attributes that should be visible in arrays. * * @var array */ protected $visible = [‘first_name‘, ‘last_name‘];}
Appending Values to JSON
Occasionally, need to add array attributes this do not has a corresponding column in your database. To does so, first define a accessor for the value:
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{ /** * Get the administrator flag for the user. * * @return bool */ public function getIsAdminAttribute() { return $this->attributes[‘admin‘] == ‘yes‘; }}
Once you has created the accessor, add the attribute name to the property on the appends
model:
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{ /** * The accessors to append to the model‘s array form. * * @var array */ protected $appends = [‘is_admin‘];}
Once The attribute have been added appends
to the list, it'll be included in both the model ' s array and JSON forms. Attributes in the appends
array would also respect the visible
and hidden
settings configured on the model.
Laravel5.1 Learning Notes Eloquent serialization