如何在 Laravel 5 中为表使用全局前缀

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

How to use global prefix for tables in Laravel 5

laravellaravel-5laravel-5.2artisanartisan-migrate

提问by aalaap

New in Laravel. Probably a silly question. I had setup database like this:

Laravel 中的新功能。大概是个愚蠢的问题。我有这样的设置数据库:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => 'mydb',
        'username' => 'myusername',
        'password' => 'mypassword',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => 'admin',
        'strict' => false,
        'engine' => null,
    ],

Notice 'prefix' => 'admin'. This is because I want all tables related to the website's control panel be prefixed with admin, e.g: admin_users, admin_log, etc...

注意'prefix' => 'admin'。这是因为我希望与网站控制面板相关的所有表都以admin为前缀,例如:admin_users、admin_log 等...

But I'm stuck at the very beginning. I'm trying to create migrations via artisan but it's not creating the tables with the prefix.

但我一开始就卡住了。我正在尝试通过 artisan 创建迁移,但它没有创建带有前缀的表。

php artisan make:migration create_users_table --create=users

I'm expecting that to create a table named admin_users. But it's not.

我期待创建一个名为admin_users. 但事实并非如此。

Am I doing this right?

我这样做对吗?

回答by aalaap

Laravel caches config files, so you may simply need to clear the cache:

Laravel 缓存配置文件,所以你可能只需要清除缓存:

php artisan config:clear

In addition to this, a better practice is to use the .envfile to define your prefix, like this:

除此之外,更好的做法是使用该.env文件来定义您的前缀,如下所示:

...
DB_PREFIX=admin_
...

and loading that in your config file, like this:

并将其加载到您的配置文件中,如下所示:

'prefix' => env('DB_PREFIX', 'abc')

This is how it should have been by default.

默认情况下应该是这样。

You're already loading the hostname and port from the .envfile, so why not do it for the other values as well?

您已经从.env文件中加载了主机名和端口,那么为什么不也为其他值加载呢?

回答by tnash

The migrations will be created without a prefix. After running php artisan migrateyou should see tables with prefixes

将在没有前缀的情况下创建迁移。运行后php artisan migrate你应该看到带有前缀的表

回答by Giedrius Kir?ys

Prefix does not includes underscore (_) by itself. In order to create admin_usersYou have to use admin_prefix.
When generating migrations You will get plain table names, without prefixes.

前缀本身不包括下划线 ( _)。为了创建admin_users你必须使用admin_前缀。
生成迁移时,您将获得没有前缀的普通表名。

With prefix = 'admin_';this: Schema::create('users', function (Blueprint $table) {<...>});will result in creation of admin_userstable, because Laravel adding Your prefix under the hood by default.

有了prefix = 'admin_';这个:Schema::create('users', function (Blueprint $table) {<...>});将导致创建admin_users表,因为 Laravel 默认在引擎盖下添加你的前缀。



TL;DR

TL; 博士

Even though Your scheme displays table name without prefix, Laravel will create it with prefix.

即使您的方案显示没有前缀的表名,Laravel 也会使用前缀创建它。