Laravel5.1学习笔记23 Eloquent 序列化
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 toArray
method. 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 toJson
method 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.
Laravel5.1学习笔记23 Eloquent 序列化的更多相关文章
- Laravel5.1学习笔记22 Eloquent 调整修改
Eloquent: Mutators Introduction Accessors & Mutators Date Mutators Attribute Casting Introductio ...
- Ext.Net学习笔记23:Ext.Net TabPanel用法详解
Ext.Net学习笔记23:Ext.Net TabPanel用法详解 上面的图片中给出了TabPanel的一个效果图,我们来看一下代码: <ext:TabPanel runat="se ...
- [原创]java WEB学习笔记23:MVC案例完整实践(part 4)---模糊查询的设计与实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Linux下汇编语言学习笔记23 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Laravel5.1学习笔记18 数据库4 数据填充
简介 编写数据填充类 使用模型工厂类 调用额外填充类 执行填充 #简介 Laravel includes a simple method of seeding your database with t ...
- Python学习笔记23:Django构建一个简单的博客网站(一个)
在说如何下载和安装Django,本节将重点讨论如何使用Django站点. 一 新建project 命令:django-admin startproject mysite # 有的须要输入:django ...
- Kali学习笔记23:Web渗透简介
文章的格式也许不是很好看,也没有什么合理的顺序 完全是想到什么写一些什么,但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习,大牛和杠精们请绕道 我这里先说几句: 其实从缓冲区溢出到Web渗透之间还有 ...
- Java学习笔记42(序列化流)
对象中的数据,以流的形式,写入到文件中保存 过程称为写出对象,对象的序列化 ObjectOutputStream将对象写到文件中,实现序列化 在文件中,以流的形式,将对象读取出来, 读取对象,对象的反 ...
- C++学习笔记23,类内函数重载
该博文仅用于交流学习.请慎用于不论什么商业用途,本博主保留对该博文的一切权利. 博主博客:http://blog.csdn.net/qq844352155 转载请注明出处: 在一个类内,最常见的就是构 ...
随机推荐
- 绿盟NF防火墙系统
http://www.nsfocus.com.cn/ http://www.nsfocus.com.cn/products/details_22_5.html
- html5 编辑
在html中想获得矢量图形可以用svg标签.该标签画出的图形全部用代码实现. 可以用在线html编辑工具来进行所见即所得编辑,然后到处源码. 比较好用的工具有http://editor.method. ...
- 关于ping以及TTL的分析
首先介绍一下ping这个工具 ping [目标] 的意思就是向目标发送几个数据包,之后假设目标接受到一个数据包.那么目标就会向发送ping的主机返回一个数据包 比方上图.我ping了百度的server ...
- linux驱动之中断方式获取键值
linux驱动之中断方式获取键值 ----------------------------------------------------------------------------------- ...
- MongoDB升级导致启动失败
起因 最近项目使用MongoDB,但是作为一个技术菜鸟,NoSQL数据库我还真不会用,于是我就在自己的阿里云服务器上安装了一个MongoDB4.0.9. 现象 但是当我使用yum -y update升 ...
- Javascript基本概念梳理
javascript里的数据类型: 原始类型:数字,字符串.布尔值.(原始值:null,undefined) 对象类型:键值对,数组,function,全局对象(MATH,JSON) 保留字: abs ...
- Python开发【第5节】【函数基础】
1.函数 函数的本质就是功能的封装. 函数的作用 提升代码的重复利用率,避免重复开发相同代码 提高程序开发效率 便于程序维护 2.函数定义 def 函数名(参数): """ ...
- YII2 的授权(Authorization)
说明:翻译本不是我应该做的,由于我的英语水平实在太差.但由于对YII的兴趣.所以也做一点.同一时候也能显示出我的胆量还是有的...希望不误导您. 由于这里MD语法的内容显示不全.您能够去这里看看. A ...
- 【bzoj2600】 [Ioi2011]ricehub
如果发现尾指针到头指针这段稻田的中位数上建一个粮仓时距离之和超过了B 就调整尾指针对距离维护一个前缀和 每次取中位数之后可以O(1)计算距离和 #include<algorithm> #i ...
- 纯css3实现美化复选框和手风琴效果(详细)
关键技术点和原理: 原理就是把 checkbox或 radio 给隐藏掉 ,然后给选框 绑定一个label标签. 然后用label标签作为容器,在里面放一个:before或一个after 用bef ...