Laravel 中的数据库/模型字段名称约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21400711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Database/model field-name convention in Laravel?
提问by preyz
I understand that camel case is the usual convention, as per http://forums.laravel.io/viewtopic.php?id=8857
我知道骆驼案例是通常的约定,根据http://forums.laravel.io/viewtopic.php?id=8857
However, Laravel is using created_at and updated_at for timestamp fields.
但是,Laravel 使用 created_at 和 updated_at 作为时间戳字段。
I'm confused as what would be the best naming convention for field-names in the database? If camel cased, can I then switch created_at to createdAt, or should I use camel case for own fields and snake case for the timestamps?
我很困惑,数据库中字段名称的最佳命名约定是什么?如果采用驼峰式编码,我可以将 created_at 切换为 createdAt,还是应该将驼峰式用于自己的字段,将蛇式用于时间戳?
It doesn't seem clear to me. Maybe Laravel wants me to use snake case all the way, like first_name in the database and $first_name in the PHP code, rather than firstName in the database, and $firstName in the code?
对我来说似乎不太清楚。也许 Laravel 想让我一直使用蛇形大小写,比如数据库中的 first_name 和 PHP 代码中的 $first_name,而不是数据库中的 firstName 和代码中的 $firstName?
I hope someone can clarify...
我希望有人能澄清...
回答by Alexandre Butynski
Snake case is the convention for database fields and model attributes.
Snake case 是数据库字段和模型属性的约定。
You can see it in the getter and setter (http://laravel.com/docs/eloquent#accessors-and-mutators) or relationships sections (http://laravel.com/docs/eloquent#relationships) in the documentation.
您可以在文档的 getter 和 setter ( http://laravel.com/docs/eloquent#accessors-and-mutators) 或关系部分 ( http://laravel.com/docs/eloquent#relationships) 中看到它。