Skip to content

Latest commit

 

History

History
111 lines (78 loc) · 3.27 KB

File metadata and controls

111 lines (78 loc) · 3.27 KB

模型序列化

介绍

构建JSON API时,通常需要将模型和关系转换为数组或JSON。 模型包括进行这些转换的便捷方法,以及控制序列化中包含哪些属性。

基础用法

将模型转换为数组

要将模型及其加载的relationships转换为数组,可以使用toArray方法。 此方法是递归的,因此所有属性和所有关系(包括关系关系)都将转换为数组:

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

return $user->toArray();

您还可以将collections 转换为数组:

$users = User::all();

return $users->toArray();

将模型转换为JSON

要将模型转换为JSON,可以使用toJson方法。 与toArray一样,toJson方法是递归的,因此所有属性和关系都将转换为JSON:

$user = User::find(1);

return $user->toJson();

或者,您可以将模型或集合强制转换为字符串,该字符串将自动调用toJson方法:

$user = User::find(1);

return (string) $user;

由于模型和集合在转换为字符串时会转换为JSON,因此可以直接从应用程序的路由,AJAX处理程序或控制器返回Model对象:

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

隐藏JSON中的属性

有时,您可能希望限制模型数组或JSON表示中包含的属性(如密码)。 为此,请在模型中添加$hidden属性定义:

<?php namespace Acme\Blog\Models;

use Model;

class User extends Model
{
    /**
     * 应该为数组隐藏的属性。
     *
     * @var array
     */
    protected $hidden = ['password'];
}

或者,您可以使用$visible属性来定义应包含在模型的数组和JSON表示中的属性的白名单:

class User extends Model
{
    /**
     * 应该在数组中可见的属性。
     *
     * @var array
     */
    protected $visible = ['first_name', 'last_name'];
}

将值附加到JSON

有时,您可能需要添加数据库中没有相应列的数组属性。 为此,首先为值定义转换器

class User extends Model
{
    /**
     * 获取用户的管理员标志。
     *
     * @return bool
     */
    public function getIsAdminAttribute()
    {
        return $this->attributes['admin'] == 'yes';
    }
}

创建访问器后,将属性名称添加到模型的appends属性中:

class User extends Model
{
    /**
     * 要附加到模型的数组形式的访问器。
     *
     * @var array
     */
    protected $appends = ['is_admin'];
}

将属性添加到appends列表后,它将包含在模型的数组和JSON表单中。 appends数组中的属性也将遵循模型上配置的visiblehidden设置。