Ruhul
Ruhul

Reputation: 81

Laravel Database Seeder Class

Here is my DatabaseSeeder Class Code

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

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


        $this->call(
            AdminSeeder::class,
            CategorySeeder::class,
            UsersSeeder::class,);
    }
}

My php Artisan Command is: php artisan db:seed I want to migrate all Seeder class by one commad. but I can't do it. pls help me.

Upvotes: 3

Views: 1823

Answers (2)

LF-DevJourney
LF-DevJourney

Reputation: 28529

You also call each seeder seperately.

$this->call('AdminSeeder');
$this->call('CategorySeeder');
$this->call('UsersSeeder');

Edit for the downvoter, the call function can accept array or string.

/**
 * Seed the given connection from the given path.
 *
 * @param  array|string  $class
 * @param  bool  $silent
 * @return $this
 */
public function call($class, $silent = false)
{
    $classes = Arr::wrap($class);
    foreach ($classes as $class) {
        if ($silent === false && isset($this->command)) {
            $this->command->getOutput()->writeln("<info>Seeding:</info> $class");
        }
        $this->resolve($class)->__invoke();
    }
    return $this;
}

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

The call() method expects an array, not a list of arguments, so the proper invocation is

    $this->call([
        AdminSeeder::class,
        CategorySeeder::class,
        UsersSeeder::class,
    ]);

The key here is that array is accepted since version 5.5 of Laravel framework. Previously, including v5.4 you are now using, only allowed single class name (string) as argument. So if you cannot upgrade to 5.5, you need to call all the classes separately, i.e.:

    $cls = [
        AdminSeeder::class,
        CategorySeeder::class,
        UsersSeeder::class,
    ];
    foreach ($cls as $c) {
       $this->call($c);
    }

Docs for v5.4 and docs for v5.5

Upvotes: 2

Related Questions