Laravel 5.1 刷新并播种单表

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

Laravel 5.1 refresh and seed a single table

laravellaravel-5seeding

提问by V4n1ll4

I'm looking to refresh and seed a single table in Laravel 5.1. Is this even possible?

我希望在 Laravel 5.1 中刷新和播种单个表。这甚至可能吗?

I have tried the below, but it gives an error (incorrect syntax).

我已经尝试了下面的方法,但它给出了一个错误(不正确的语法)。

php artisan migrate:refresh --path=database/migrations/CreateTableTimesheet

If I use: php artisan migrate:refreshit just says:

如果我使用:php artisan migrate:refresh它只是说:

Nothing to migrate

没有什么可迁移的

回答by Zakaria Acharki

You could use migrate:refreshcommand that will roll back all of your migrations and then execute the migratecommand. This command effectively re-creates your entire database :

您可以使用migrate:refresh将回滚所有迁移的migrate命令,然后执行该命令。此命令有效地重新创建了整个数据库:

php artisan migrate:refresh

And you may use the --classoption to specify a specific seeder class to run individually :

您可以使用该--class选项指定要单独运行的特定播种机类:

php artisan db:seed --class=UserTableSeeder

The full code will be :

完整的代码将是:

php artisan migrate:refresh
php artisan db:seed --class=UserTableSeeder

Hope this helps.

希望这可以帮助。

回答by web_developer

Its better to truncate your same table first and then seed:-

最好先截断同一张表,然后再播种:-

public function run()
{
    Table::truncate();
    //seed your table here
}

then you can run your same seeder like this:-

然后你可以像这样运行你的同一个播种机:-

php artisan db:seed --class=YourSeeder

回答by web_developer

Maybe first just backup the database, drop it and check if whole seeding, migrating and refreshing mechanic works. But first dump artisan autoload.

也许首先只是备份数据库,删除它并检查整个播种、迁移和刷新机制是否有效。但首先转储工匠自动加载。

回答by Frank Decker

php artisan tinker

>>> App\Status::truncate()

Will clear the statuses table

将清除状态表

So you can do

所以你可以做


>>> App\{MODEL_CLASS}::truncate()



I found this quite useful when I don't want to clear all the tables, especially users.

当我不想清除所有表,尤其是用户时,我发现这非常有用。

回答by oriberu

I don't think any answer so far addresses the question of migrating and seeding a singletable. So, given the migration file database/migrations/create_foo_table.phpand the seed file database/seeds/FooTableSeeder.php, which contains the FooTableSeederseed class, you can do the following:

我认为到目前为止没有任何答案可以解决迁移和播种单个表的问题。因此,给定迁移文件database/migrations/create_foo_table.phpdatabase/seeds/FooTableSeeder.php包含FooTableSeeder种子类的种子文件,您可以执行以下操作:

php artisan migrate:refresh --path=database/migrations/create_foo_table.php
php artisan db:seed --class=FooTableSeeder

This will rollback, migrate and seed your Foo table. See: rolling back migrationsand running seedersin the Laravel 5.1 documentation (at the time of writing this, Laravel 7.x is out and the syntax has not changed).

这将回滚、迁移和播种您的 Foo 表。请参阅:Laravel 5.1 文档中的回滚迁移运行播种机(在撰写本文时,Laravel 7.x 已发布且语法未更改)。