Laravel 5:修改模型中列名的最佳方法

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, ...];