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.

Basic Usage

Converting A Model To An Array

To convert a model and its loaded relationships to an array, you may use the toArraymethod. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays:

$user = App\User::with('roles')->first();

return $user->toArray();

You may also convert collections to arrays:

$users = App\User::all();

return $users->toArray();
Converting A Model To JSON

To convert a model to JSON, you may use the toJson method. Like toArray, the toJsonmethod is recursive, so all attributes and relations will be converted to JSON:

$user = App\User::find(1);

return $user->toJson();

Alternatively, you may cast a model or collection to a string, which will automatically call the toJson method:

$user = App\User::find(1);

return (string) $user;

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:

Route::get('users', function () {
return App\User::all();
});

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 definition to your model:

<?php

namespace 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 may use the visible property to define a white-list of attributes that should be included in your model's array and JSON representation:

<?php

namespace 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, you may need to add array attributes that do not have a corresponding column in your database. To do so, first define an accessor for the value:

<?php

namespace 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 have created the accessor, add the attribute name to the appends property on the model:

<?php

namespace 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 has been added to the appends list, it will be included in both the model's array and JSON forms. Attributes in the appends array will also respect thevisible and hidden settings configured on the model.

最新文章

  1. 【linux使用】bash shell命令行常用快捷键 (转载)
  2. 未能正确加载“RoslynPackage”包
  3. 2016暑假多校联合---Joint Stacks (STL)
  4. 【BZOJ 3165】【HEOI 2013】Segment
  5. Sqlserver自定义函数Function
  6. Block 传值
  7. windows下启动mongodb
  8. USACO 3.3 fence 欧拉回路
  9. Android SeekBar自定义使用图片和颜色显示
  10. 【C#】添加鼠标管轮事件
  11. 单元测试工具之Xunit
  12. 【JPA】两种不同的实现jpa的配置方法
  13. Amazon EC2 的名词解释
  14. jsp 按钮颜色
  15. 九度OJ 1014 排名
  16. 网络中的NAT模式
  17. 洛谷1855 榨取kkksc03
  18. confidence interval
  19. Java设计模式从精通到入门五 抽象工厂方法模式
  20. mysql 时间戳的使用!

热门文章

  1. Ubuntu 16.04出现Can&#39;t open /etc/rc.d/init.d/functions的问题解决
  2. Ubuntu 16.04 LTS GNOME版本下载
  3. laravel 实时facade
  4. VBS 操作Word
  5. UVA - 11374 Airport Express (Dijkstra模板+枚举)
  6. 读书笔记:Information Architecture for the World Wide Web, 3rd Edition 北极熊 简介
  7. (五)Java 对象和类
  8. Email-ext plugin
  9. Called attach on a child which is not detached
  10. 详解jQuery uploadify文件上传插件的使用方法