工匠,在数据库中创建表 [英] Artisan, creating tables in database
问题描述
我试图在Laravel 5中创建mysql表.我在/project/database/migrations
中创建了一个名为users.php
的文件:
I am trying to create mysql tables in Laravel 5. I created a file in /project/database/migrations
called users.php
:
[...]
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('fullname');
$table->int('number');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
[...]
然后我尝试在project
文件夹中运行以下命令:
I then tried running these commands in the project
-folder:
$ php artisan migrate
$ php artisan migrate:install
$ php artisan migrate --pretend
它们都不返回 any 输出,并且不创建任何表.存在要填充的数据库.
None of them return any output and no tables are created. The database to be populated exists.
推荐答案
迁移文件必须与模式*_*.php
匹配,否则将找不到它们.由于users.php
与该模式不匹配(没有下划线),因此迁移程序将找不到该文件.
Migration files must match the pattern *_*.php
, or else they won't be found. Since users.php
does not match this pattern (it has no underscore), this file will not be found by the migrator.
理想情况下,您应该使用artisan创建迁移文件:
Ideally, you should be creating your migration files using artisan:
php artisan make:migration create_users_table
这将创建具有适当名称的文件,然后您可以对其进行编辑以充实您的迁移.该名称还将包括时间戳记,以帮助迁移者确定迁移顺序.
This will create the file with the appropriate name, which you can then edit to flesh out your migration. The name will also include the timestamp, to help the migrator determine the order of migrations.
您还可以使用--create
或--table
开关添加更多样板,以帮助您入门:
You can also use the --create
or --table
switches to add a little bit more boilerplate to help get you started:
php artisan make:migration create_users_table --create=users
有关迁移的文档可在此处找到.
The documentation on migrations can be found here.
这篇关于工匠,在数据库中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!