首页 > 解决方案 > 更改模型属性名称

问题描述

在我的数据库中,名称是蛇形的。你怎么能把它们翻译成像 CamelCase 这样的模型。

我用

public function getAttributes($key) {
        if (array_key_exists($key, $this->relations)) {
            return parent::getAttribute($key);
        } else {
            return parent::getAttribute(Str::snake($key));
        }
    }

但是在转储集合或模型中,我在蛇盒中得到了名字。

标签: laravelmodellaravel-8

解决方案


使用 Laravel 字符串助手“camel()”将蛇案例转换为来案例

使用 Illuminate\Support\Str;

$converted = Str::of('foo_bar')->camel();

// fooBar

参考:https ://laravel.com/docs/8.x/helpers#method-fluent-str-camel


推荐阅读