'Laravel 5.1 refresh and seed a single table
I'm looking to refresh and seed a single table in Laravel 5.1. Is this even possible?
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:refresh
it just says:
Nothing to migrate
Solution 1:[1]
You could use migrate:refresh
command that will roll back all of your migrations and then execute the migrate
command. This command effectively re-creates your entire database :
php artisan migrate:refresh
And you may use the --class
option to specify a specific seeder class to run individually :
php artisan db:seed --class=UserTableSeeder
The full code will be :
php artisan migrate:refresh
php artisan db:seed --class=UserTableSeeder
Hope this helps.
Solution 2:[2]
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
Solution 3:[3]
Maybe first just backup the database, drop it and check if whole seeding, migrating and refreshing mechanic works. But first dump artisan autoload.
Solution 4:[4]
I don't think any answer so far addresses the question of migrating and seeding a single table. So, given the migration file database/migrations/create_foo_table.php
and the seed file database/seeds/FooTableSeeder.php
, which contains the FooTableSeeder
seed class, you can do the following:
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 migrations and running seeders in the Laravel 5.1 documentation (at the time of writing this, Laravel 7.x is out and the syntax has not changed).
Solution 5:[5]
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.
Solution 6:[6]
You can do it in two steps:
- Refresh your specific table:
php artisan migrate:refresh --path=database/migrations/00_create_foo_table.php
- Seed tables set in
database/seeds/DatabaseSeeder.php
:
composer dump-autoload
php artisan db:seed
=== Extra information ===
You can comment the seeders you don't want to use in DatabaseSeeder.php
:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
FooSeeder::class,
// BarSeeder::class,
// UserSeeder::class,
]);
}
}
In this example, only database/seeds/FooSeeder.php
will be passed.
Solution 7:[7]
include full table name
example
php artisan migrate:refresh --path='database/migrations/2014_10_12_000000_create_table_timesheet.php'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | harbrinder_singh |
Solution 3 | |
Solution 4 | oriberu |
Solution 5 | Frank Decker |
Solution 6 | |
Solution 7 | krishna |