laravel - 更改模型属性名称
问题描述
在我的数据库中,名称是蛇形的。你怎么能把它们翻译成像 CamelCase 这样的模型。
我用
public function getAttributes($key) {
if (array_key_exists($key, $this->relations)) {
return parent::getAttribute($key);
} else {
return parent::getAttribute(Str::snake($key));
}
}
但是在转储集合或模型中,我在蛇盒中得到了名字。
解决方案
使用 Laravel 字符串助手“camel()”将蛇案例转换为来案例
使用 Illuminate\Support\Str;
$converted = Str::of('foo_bar')->camel();
// fooBar
参考:https ://laravel.com/docs/8.x/helpers#method-fluent-str-camel
推荐阅读
- php - 调试 SMTP 邮件
- asp.net-mvc - ASP.NET MVC 5 HttpPostedFileBase 在模型中始终为空
- html - bootstrap :当列向下时生成空白区域以进行响应式使用
- react-native - source.pipe 不是带有 redux-observable / rxjs/operators 的函数
- nunit - NUnit.Engine.NUnitEngineUnloadException :遇到多个异常。检索 AggregatedExceptions 属性以获取更多信息
- android - 崩溃时的 Crashlytics customkey 报告
- json - 如何在不排序的情况下解析 QJson 文件?
- python - 带有unicode标志的python正则表达式奇怪的长度问题
- java - 如何使用 for 循环保存在数组中并检查 Java 中的现有值
- android - 单个活动多个片段导航问题