模型获取器的使用
const STATUS_PAY = 0;
const STATUS_PAY_SUCCESS = 1;
const STATUS_PAY_FAIL = 2;
protected $appends = ['status_text'];
public function getStatusTextAttribute(){
$status = $this->attributes['status']
$text = '';
switch($status){
case:0
$text = '待付款';
break;
case:1
$text = '付款成功';
break;
case:2
$text = '付款失败';
break;
default:
$text = '未知错误';
break;
}
return $text;
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
一般不这么用,枚举翻译,用语言包。模型枚举值转换也用
$cast
属性。