0
votes

Hi developers i have question regarding on php artisan migrate:refresh --seed VS php artisan db:seed I just wanted to ask because I have problem on php artisan migrate:refresh --seed, however when I use the php artisan db::seed it works properly

Now the data that I Created on my seeder is not seeding to the tables. I don't know why where the problem came from

Seeding: VehicleModelsSeeder

Seeded: VehicleModelsSeeder (0 seconds)

Here is my vehicle model seeder class

    <?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use App\Vehicle;
use App\VehicleModel;

class VehicleModelsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //

        $vehicles = Vehicle::all();
        foreach($vehicles as $vehicles_data) {
            VehicleModel::forceCreate([
                'name' => Str::random(5),
                'vehicle_id' => $vehicles_data->id
            ]);

        }
        

    }
}
1

1 Answers

1
votes

By default, the db:seed command runs the Database\Seeders\DatabaseSeeder class.

There is two solutions:

1. You need to call your additional seeder(s) in the default seeder's run method.

database/seeders/DatabaseSeeder.php

public function run()
    {
        $this->call([
            VehicleModelsSeeder::class
        ]);
    }

Then:

php artisan migrate:refresh --seed

2. You can specify which seeder you want to run with the --class flag, but in this case you need to run the refresh and the migrate commands separately:

php artisan migrate:refresh
php artisan db:seed --class:VehicleModelsSeeder

More info: Laravel database seeding documentation