Laravel:对于 Eloquent 关系方法,我应该坚持使用驼峰命名法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46844910/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:50:27  来源:igfitidea点击:

Laravel: Should I stick with camel case names for Eloquent relationship methods?

laraveleloquent

提问by Kim Prince

Laravel encourages us to use snake_case, e.g. first_name, for model attribute names. In particular, when snake case is used to access an attribute from outside the class, it will automatically look for an accessor named getFirstNameAttribute.

Laravel 鼓励我们使用snake_case,例如first_name,作为模型属性名称。特别是,当使用蛇形大小写从类外部访问属性时,它会自动查找名为 getFirstNameAttribute 的访问器。

When it comes to model relationships however, it seems more natural to use camel case. For example, if a stadium has multiple access points, then the stadiumclass might have an accessPoints()method. I can call this method as a property ($stadium->accessPoints) to retrieve a list of access points, or I can call it as a method ($stadium->accessPoints()) to get an instance of the underlying query builder.

然而,当谈到模型关系时,使用驼峰式似乎更自然。例如,如果体育场有多个接入点,则stadium该类可能有一个accessPoints()方法。我可以将此方法作为属性 ( $stadium->accessPoints)调用以检索访问点列表,也可以将其作为方法 ( $stadium->accessPoints())调用以获取基础查询构建器的实例。

This is different to how I would normally approach naming conventions. I would normally name attributes using the same case (either snake_case, or camelCase), irrespective of how the attribute is realised.

这与我通常处理命名约定的方式不同。我通常会使用相同的大小写(snake_case 或camelCase)命名属性,而不管属性是如何实现的。

I am now embarking on a large Laravel project. Should I stick with the two different syntaxes, or am I likely to regret it down the track?

我现在正在着手一个大型的 Laravel 项目。我应该坚持使用两种不同的语法,还是我可能会后悔?

回答by gbalduzzi

Well, it is a convention, so you should use the approach you are more comfortable with.

嗯,这是一个约定,所以你应该使用你更习惯的方法。

I personally use snake_case only on column attributes and camelCase on everything else (class names, relationships ecc).

我个人只在列属性上使用snake_case,在其他一切(类名、关系ecc)上使用camelCase。

Note that camelCase for class names and methods is basically necessary, because all laravel and external modules classes and methods use camelCase. If you use snake_case, it will be a mess with some methods implemented by you in snake_case and framework methods in camelCase.

注意类名和方法的camelCase基本上是必要的,因为所有laravel和外部模块的类和方法都使用camelCase。如果你使用snake_case,你在snake_case 中实现的一些方法和camelCase 中的框架方法会一团糟。

For relationships, you will basically only use relationships defined by yourself, so i believe you can really pick the one you prefer.

对于关系,你基本上只会使用你自己定义的关系,所以我相信你真的可以选择你喜欢的。

回答by Taha Paksu

There's no true convention, like in Assassin's Creed's saying,

没有真正的惯例,就像刺客信条所说的那样,

"Nothing is true, everything is permitted"

“没有什么是真的,一切都是允许的”

Laravel follows the PSR-2 coding standard. Source: https://laravel.com/docs/5.5/contributions

Laravel 遵循 PSR-2 编码标准。来源:https: //laravel.com/docs/5.5/contributions

Which redirects us to https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md

这将我们重定向到https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md

(from https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.mdwhich says: Code MUST follow all rules outlined in PSR-1.)

(来自https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md说:代码必须遵循 PSR-1 中列出的所有规则。

And there it says:

它说:

Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.

无论使用什么命名约定,都应该在合理的范围内一致应用。该范围可以是供应商级别、包级别、类级别或方法级别。

And from that declaration, that's related to separation between environment level conventions, such as database design conventions and code conventions. Most database design conventions encourage snake case column names, and most code conventions use camel case method names, class names etc.

从那个声明来看,这与环境级别约定之间的分离有关,例如数据库设计约定和代码约定。大多数数据库设计约定鼓励使用蛇形大小写的列名,并且大多数代码约定使用驼峰式方法名、类名等。

And when defining a model, to declare that, that the model property relates with a database column, the coding convention changes from camelCaseto snake_case.

在定义模型时,为了声明模型属性与数据库列相关,编码约定从camelCase更改为snake_case