laravel让你自定义的访问器属性主动显示出来
2019-02-27 本文已影响0人
空气KQ
定义一个访问器
get名字驼峰式Attribute
例如:getStatusNameAttribute对应的是status_name,如果是获取的时候,获取自动调用这个访问器
,如果你没有获取是不会调用的,数组是不会自动显示
,我们需要在数组的时候显示出来,这个时候就需要主动显示
在类型里面附加进去,主动显示
protected $appends = ['check_status_name'];
public function getCheckStatusNameAttribute()
{
$staus=[
1=>'审核通过',
0=>'审核失败',
2=>'等待审核'
];
return $staus[$this->check_status];
}
演示结果不带附加测试
Array
(
[id] => 3
[sort] => 0
[next_ratio] => 0
[origin_password] => 123456
[password] => $2y$10$eDfojSSpu.pIdmbGYtNMseprIegJj.lojfV9FFkYZn94PGXjiImzK
[name] => 空气工作室
[ip] => 127.0.0.1
[host] => www.kong-qi.com
[app_key] => 20181230xxx
[token] => Uj4NdinacVQxxx
[draw_type] => 2
[min_money] => 4444
[realname] => 空气
[idcard] =>
[address] => gddddg
[email] => kq@qq.com
[mobile] => 1310454614
[ratio] => 12
[qq] => 531833998
[is_proxy] => 1
[from_id] => 0
[is_checked] => 1
[login_status] => 1
[last_number] => 4
[login_ip] => 127.0.0.1
[last_time] => 2019-01-28 23:23:41
[remember_token] => 9zpfmAYrltvkRAhGo2h8HCkOxNxIAdNhpsjYRXdpnAuo2lLDUemAcAWNv5UF
[check_status] => 1
[check_at] =>
[created_at] => 2018-12-30 14:25:15
[updated_at] => 2019-01-28 23:23:41
[deleted_at] =>
)
数组并没有显示出来,好了附加上去看看
Array
(
[id] => 3
[sort] => 0
[next_ratio] => 0
[origin_password] => 123456
[password] => $2y$10$eDfojSSpu.pIdmbGYtNMseprIegJj.lojfV9FFkYZn94PGXjiImzK
[name] => 空气工作室
[ip] => 127.0.0.1
[host] => www.kong-qi.com
[app_key] => 20181230144141yOSwgi
[token] => Uj4NdinacVQWU3mXjkRM1sNdbDH0DFPH
[draw_type] => 2
[min_money] => 4444
[realname] => 空气
[idcard] =>
[address] => gddddg
[email] => kq@qq.com
[mobile] => 1310454614
[ratio] => 12
[qq] => 531833998
[is_proxy] => 1
[from_id] => 0
[is_checked] => 1
[login_status] => 1
[last_number] => 4
[login_ip] => 127.0.0.1
[last_time] => 2019-01-28 23:23:41
[remember_token] => 9zpfmAYrltvkRAhGo2h8HCkOxNxIAdNhpsjYRXdpnAuo2lLDUemAcAWNv5UF
[check_status] => 1
[check_at] =>
[created_at] => 2018-12-30 14:25:15
[updated_at] => 2019-01-28 23:23:41
[deleted_at] =>
[check_status_name] => 审核通过
)
有了,就可以调用他了。
运行时追加
return $user->append('is_admin')->toArray();
return $user->setAppends(['is_admin'])->toArray();