Laravel 迁移命名约定

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

Laravel Migrations Naming Convention

phplaravelnaming-conventionsdatabase-migration

提问by Noman Ur Rehman

Is there a naming convention or guide that one should follow while naming Laravel migrations or should the name only be descriptive enough ?

在命名 Laravel 迁移时是否应该遵循命名约定或指南,或者该名称是否应该仅具有足够的描述性?

Also, suppose you are adding 12 columns to modify a table then in such a case the migration name would be too long if made descriptive so are there any guide lines to follow ?

另外,假设您要添加 12 列来修改表,那么在这种情况下,如果进行描述,迁移名称会太长,那么是否有任何指导方针可以遵循?

采纳答案by Margus Pala

It should be descriptive enough for you to check back and understand what did you do with DB in this migration.

它应该具有足够的描述性,以便您回顾并了解您在这次迁移中对 DB 做了什么。

If you start migration with table_ then Laravel adds Schema::create. If you have toor fromor inthen Laravel creates Schema::tablefor you. This makes you life easier.

如果你使用 table_ 开始迁移,那么 Laravel 会添加Schema::create. 如果你必须进入,那么 Laravel 会Schema::table为你创建。这让您的生活更轻松。

I usually name the migrations based on feature eg implement_user_roles or make_employee_profile_editable.

我通常根据功能命名迁移,例如implement_user_roles 或make_employee_profile_editable。

回答by Vitalii

According to \Illuminate\Database\Console\Migrations\TableGuesserclass source there are two default patterns to guess migration table and stub type.

根据\Illuminate\Database\Console\Migrations\TableGuesser类源有两种默认模式来猜测迁移表和存根类型。

const CREATE_PATTERNS = [
    '/^create_(\w+)_table$/',
    '/^create_(\w+)$/',
];

const CHANGE_PATTERNS = [
    '/_(to|from|in)_(\w+)_table$/',
    '/_(to|from|in)_(\w+)$/',
];

回答by Mete Kabak

Make sure you don't use the same class name with your model, it still works but for understanding,

确保你的模型没有使用相同的类名,它仍然有效,但为了理解,

Put create_ if you are creating for example,or

例如,如果您正在创建,则放置 create_,或者

If the tables are pivot to each other make it like article_comment to make sure you will understand when you try to change it 5 months later :)

如果表格相互关联,请使其像 article_comment 一样,以确保您在 5 个月后尝试更改时会理解:)