讨论数量:
假设数据库中字段命名都是蛇形法(如user_name、join_time之类),而想要在访问属性时使用驼峰法(也就是$user->userName、joinTime)。那么可以使用以下方法:
- 在所有继承了Eloquent\Model的类上面加一层baseModel类
- 在baseModel类中实现getAttribute方法,如下:
public function getAttribute($key)
{
$key = Str::snake($key);
return parent::getAttribute($key);
}
推荐文章: