3

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.

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

but this $this->call([ AdminSeeder::class, CategorySeeder::class, UsersSeeder::class, ]); process didn't work. Array to string conversion error. How can i solve it?
0

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;
}

1 Comment

Hi the downvoter, can you show your reason here? Have you try it before downvote. This works well from laravel4.1 ~ laravel5.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.