有时候您可能想要增加不存在数据库字段的属性数据。这时候只要定义一个获取器即可:
public function getIsAdminAttribute(){ return $this->attributes['admin'];}
定义好获取器之后,再把对应的属性名称加到模型里的 appends 属性:
protected $appends = ['is_admin'];
测试:
![](https://images2018.cnblogs.com/blog/1033081/201807/1033081-20180724164953803-2101143030.png)
![](https://images2018.cnblogs.com/blog/1033081/201807/1033081-20180724165048202-343714726.png)
本文共 289 字,大约阅读时间需要 1 分钟。
有时候您可能想要增加不存在数据库字段的属性数据。这时候只要定义一个获取器即可:
public function getIsAdminAttribute(){ return $this->attributes['admin'];}
定义好获取器之后,再把对应的属性名称加到模型里的 appends 属性:
protected $appends = ['is_admin'];