What is the best way to modify column name before retrieve data from the model, ex: change id
to be uid
$data = user::all(); //return array('uid'=>1,'uid'=>2 ... etc)
thanks,
What is the best way to modify column name before retrieve data from the model, ex: change id
to be uid
$data = user::all(); //return array('uid'=>1,'uid'=>2 ... etc)
thanks,
收起
The easiest option would be to create a custom attribute getter. This will allow you to access the uid
value, like you would any other attribute, and the value will also be accessible via the toArray()
method.
class User extends Model
{
// ...
public $appends = [
'uid',
];
public function getUidAttribute()
{
return $this->attributes['id'];
}
// ...
}
$user->uid; // 1
$user->toArray() // [..., 'uid' => 1, ...];
报告相同问题?