34

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

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
V4n1ll4
  • 5,973
  • 14
  • 53
  • 92

8 Answers8

38

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.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
15

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
4

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

3

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).

oriberu
  • 1,186
  • 9
  • 6
1
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.

1

include full table name
example

php artisan migrate:refresh --path='database/migrations/2014_10_12_000000_create_table_timesheet.php'
Tejas
  • 48
  • 2
  • 8
1

Tips for run migration,roleback,refresh and seeder specific file for Laravel Framework

Migrate

php artisan migrate --path=/database/migrations/fileName.php

Roolback

php artisan migrate:rollback --path=/database/migrations/fileName.php

Refresh

php artisan migrate:refresh --path=/database/migrations/fileName.php

Seeder

php artisan db:seed --class=classNameTableSeeder

Thanks

0

You can do it in two steps:

  1. Refresh your specific table:
php artisan migrate:refresh --path=database/migrations/00_create_foo_table.php
  1. 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.

DevonDahon
  • 7,460
  • 6
  • 69
  • 114