laravel 用代码路径播种数据库?

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

Seeding database with path from code?

laravellaravel-4seeding

提问by Dan

I've been using Laravel's migrations with the path parameter like so:

我一直在使用 Laravel 的迁移和 path 参数,如下所示:

Artisan::call('migrate', array('--path' => 'path/to/my/Migrations'));

Is there anyway I can run the seed command in the same way? I have a number of seed files I want to use but I don't want to run them all at the same time.

无论如何我可以以相同的方式运行种子命令吗?我有许多想要使用的种子文件,但我不想同时运行它们。

Any advice appreciated.

任何建议表示赞赏。

Thanks

谢谢

回答by gellezzz

Instead of --path you can set --class with namespace to Seeder class.

您可以将带有名称空间的 --class 设置为 Seeder 类,而不是 --path。

Artisan::call('db:seed', [
    '--class' => 'Namespace\Seeds\DatabaseSeeder'
]);

This work on Laravel 5.1

这项在 Laravel 5.1 上的工作

回答by matthiasgiger

To refresh the migrations and seed the database, this worked for me:

要刷新迁移并为数据库设定种子,这对我有用:

// Roll back all migrations and migrate them again
Artisan::call('migrate:refresh');
// Fill tables with seeds
Artisan::call('db:seed');

I had lots of seeds and the server was slow. In this case it helps to extend the maximum execution time.

我有很多种子,服务器很慢。在这种情况下,它有助于延长最大执行时间。

// Extend maximum execution time to 3 minutes
set_time_limit(180);
Artisan::call('migrate:refresh');
Artisan::call('db:seed');
// Back to the default
set_time_limit(30);

回答by Andreyco

Seeding only

只播种

Artisan::call('db:seed');

Re-run all migration under specified path & run seeds as well

在指定路径下重新运行所有迁移并运行种子

Artisan::call('migrate:refresh', array('--path' => 'path/to/my/Migrations', '--seed'));