laravel 增加不存在数据库的字段

有时候您可能想要增加不存在数据库字段的属性数据。这时候只要定义一个获取器即可:

public function getIsAdminAttribute()
{
    return $this->attributes['admin'];
}

定义好获取器之后,再把对应的属性名称加到模型里的 appends 属性:

protected $appends = ['is_admin'];
作者:浮生若梦似水流年_
链接:https://www.jianshu.com/p/b3778e996c3a
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
 
测试:

原文地址:https://www.cnblogs.com/lxwphp/p/9361010.html