首页 > 编程 > PHP > 正文

laravel model模型处理之修改查询或修改字段时的类型格式案例

2019-11-21 19:44:06
字体:
来源:转载
供稿:网友

先了解一下此图,有助于理解

通过上图了解

这将在原有的列上添加一列is_admin,这需要通过属性访问时才会获得,如果我们希望在获得数据的时候被一起返回,则还需要append属性

class User extends Model{  //设置方法名称 protected $appends = ['is_admin','type'];  //查询时 修改 字段格式或者值 【自动触发,无需调用】 public function getIsAdminAttribute() {  return $this->attributes['title'] = 'yes'; }  //修改时 更改储存格式或者值 【自动触发,无需调用】 public function setIsAdminAttribute($value) {  //$value 代表字段的值  $this->attributes['title'] = empty($value) ? '0' : $value; }   protected $type = [1=>'aaa',2=>'bbb'];  public function getTypeAttribute() {  return $this->type[$this->attributes['type']]; } }

以上这篇laravel model模型处理之修改查询或修改字段时的类型格式案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表