1. 将想要获取的属性 添加到 Eloquent ORM模型中
protected $appends = [
'must_type',
'type_content',
];
2. 自定义属性的值
public function getMustTypeAttribute()
{
return $this->is_must == 1 ? 'Required' : 'Optional';
}
public function getTypeContentAttribute()
{
return $this->getTypes()[$this->type] ?? '';
}
3. toArray() 时, 会看到添加的属性已自动显示出来
输出 Object , 会在appends 中看到属性, 但看不到 属性值。