Laravel, how to run a different seeder to the DataBaseSeeder.php

1

I am trying to create a Seeder called users to be able to modulate the seeds for each different table and have the project better organized. The problem is that when I use the command:

php artisan db:seed

Only execute the "DatabaseSeeder.php" even if you have created another.

I have tried to create it manually and using the console with the command:

php artisan make:seeder UsersTable

The code I have in seed "UserTable" is:

<?php

use Illuminate\Database\Seeder;
use App\User;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    //Array users test
    private $users= array(
            array(  'name' => 'albert',
                    'email' => '[email protected]',
                    'password' => '1234',
                    'role' => 1),
            array(  'name' => 'test',
                    'email' => '[email protected]',
                    'password' => 'test',
                    'role' => 2),
            array(  'name' => 'test2',
                    'email' => '[email protected]',
                    'password' => 'test2',
                    'role' => 2),
            array(  'name' => 'test3',
                    'email' => '[email protected]',
                    'password' => 'test3',
                    'role' => 2),
    );

    //contador de filas creadas
    private $rows = 0;
    public function userSeed() {
         //
        for($i=0;$i<count($this->users);$i++){

                //$this->command->info($this->users[$i].' Users cargados!');
                $this->command->info($this->users[$i]['email']);
                $this->command->info($this->users[$i]['password']);
                $user_seed = new User();            
                $user_seed->name = $this->users[$i]['name'];
                $user_seed->email = $this->users[$i]['email'];
                $user_seed->password = bcrypt($this->users[$i]['password']);
                $user_seed->role = $this->users[$i]['role'];
                $user_seed->save();
                $this->rows++;
        }
    }
    //function run
    public function run()
    {
            self::userSeed();
            $this->command->info($this->rows.' :Users cargados!');


    }
}

If I have the code in "DatabaSeeder.php" it works perfectly.

Could you throw me some light?

Thanks in advance!.

    
asked by Albert Sanchez Guerrero 11.01.2018 в 23:42
source

1 answer

1

In the documentation clarify this point very clearly, you must regenerate the autoload before with the command composer dump-autoload , then you will be able to execute the first command of your question, in addition there are more options to execute a seeder specific you can do it with the command

php artisan db:seed --class=NombreDelSeeder

For your example it should be

php artisan db:seed --class=UsersTable

Or simply.

php artisan migrate:refresh --seed
    
answered by 11.01.2018 / 23:52
source