在Laravel雄辩模型中,我们使用camelCase调用表字段名称,即使它们在db中包含下划线? [英] In a Laravel Eloquent model, do we call table field names using camelCase, even though they contain an underscore in the db?

查看:247
本文介绍了在Laravel雄辩模型中,我们使用camelCase调用表字段名称,即使它们在db中包含下划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中,我在用户表中有这些字段: name favorite_color created_at 等...

In the database let's say I have in my user's table these fields: name, favorite_color, created_at, etc...

使用Eloquent保存:

Using Eloquent to save:

$user = new User;
$user->name = "John";
$user->favorite_color = 'blue';  // Notice the underscore
$user->save();



2:



2:

$user = new User;
$user->name = "John";
$user->favoriteColor = 'blue';  // camelCase
$user->save();

上述哪一项是正确的?

推荐答案

第一个是正确的表单。但是有一个包可以帮助您做第二个: https://github.com/kirkbushell/eloquence

The first one is the correct form. But there's a package to help you do the second one: https://github.com/kirkbushell/eloquence.

这篇关于在Laravel雄辩模型中,我们使用camelCase调用表字段名称,即使它们在db中包含下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆