我在使用 Laravel 构建 Web API 时遇到了问题。当我从查询中获取数据时,它的返回数据带有表的实际名称。假设如果我返回用户表数据,它看起来像这样。
{
["first_name":"John", "last_name":"Doe"],
["first_name":"Abdul", "last_name":"Karim"]
}
但我想更改表的实际列名,所以它看起来像
{
["f_name":"John", "l_name":"Doe"],
["f_name":"Abdul", "l_name":"Karim"]
}
我如何从 Laravel Eloquent ORM 中做到这一点?
最佳答案
你可以在你的模型中使用 Laravel Accessors & Mutators 更多信息请关注 http://laravel.com/docs/5.1/eloquent-mutators#accessors-and-mutators在这里,我根据您的要求给您一个示例代码
class Channel extends Eloquent {
public function getFNameAttribute()
{
return $this->attributes['first_name'];
}
}
关于php - 构建 Laravel Web API 时更改表列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33102345/