Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel passing variables from one seed file to another?

I have created multiple seed files and my main DatabaseSeeder file looks like this:

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $name1 = "James";
        $name2 = "Jeff";
        $name3 = "Joe";

        $this->call(UserTableSeeder::class);
        $this->call(PersonTableSeeder::class);
        $this->call(IndividualTableSeeder::class);
        $this->call(HumanTableSeeder::class);
    }
}

How can I make it so that UserTableSeeder and PersonTableSeeder gets the variables from my main seeder file? (What I'm really trying to do is use Faker to output random values, but use the same value for each table seeder)

like image 477
Sosa Avatar asked Sep 01 '15 23:09

Sosa


3 Answers

As of laravel 8.2 you can pass parameters to seeders natively like so

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $name1 = "James";

        $this->callWith(UserTableSeeder::class, ['name' => $name1]);
        $this->call(PersonTableSeeder::class);
        $this->call(IndividualTableSeeder::class);
        $this->call(HumanTableSeeder::class);
    }

}

class UserTableSeeder extends Seeder {
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run($name = null)
    {
        var_dump($name);
    }
}
like image 126
Tofandel Avatar answered Nov 12 '22 18:11

Tofandel


I had the same problem, ended up overriding the call() function by adding $extra var and passing it to run() function.

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $name1 = "James";
        $name2 = "Jeff";
        $name3 = "Joe";

        $this->call(UserTableSeeder::class, $name1);
        $this->call(PersonTableSeeder::class);
        $this->call(IndividualTableSeeder::class);
        $this->call(HumanTableSeeder::class);
    }

    public function call($class, $extra = null) {
        $this->resolve($class)->run($extra);

        if (isset($this->command)) {
            $this->command->getOutput()->writeln("<info>Seeded:</info> $class");
        }
    }

}

and then add $extra to your seeder class

// database/seeds/UserTableSeeder.php


  /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run($extra = null)
    {
        var_dump($extra);
    }

hope it helps.

like image 44
Or Bachar Avatar answered Nov 12 '22 16:11

Or Bachar


Laravel 8,X

Here is a combination of all the best answers on that subject were I a prompt to get the values:


// DatabaseSeeder.php


    public function run() {
        // example 1: Use directly with inside seeder
        $name     = $this->command->ask( 'Name of first user', 'Jhon Doe' );
        $email    = $this->command->ask( 'Email of first user', '[email protected]' );
        $password = $this->command->ask( 'Password of  first user', '12345678' );

        User::factory()->create( [
            'name'     => $name,
            'email'    => $email,
            'password' => Hash::make( $password )
        ] );

        // example 2: Pass value to inner-Seeder
        $answer = $this->command->ask( "Do you want to make this user ($name:$email) an admin? (yes/no)", 'Yes' );
        $this->callWith( PermissionSeeder::class, [ 'answer' => $answer ] );
    }

// PermissionSeeder.php

 public function run( int|string $answer = null ) {
       // ...
        
        $rootID = null;

        if ( isset( $answer ) && ! empty( $answer ) ) {
            if ( is_numeric( $answer ) ) {
                $rootID = (int) $answer;
            } elseif ( is_string( $answer ) && str_starts_with( strtolower( $answer ), 'y' ) ) {
                $rootID = 1;
            }
        }

        echo $rootID ? "User [$rootID] will be elevated to super user" : "[$answer] Ignore  set-user-as-root.";
        echo "";
        if ( $rootID ) {
            $user = User::findOrFail( $rootID );
            $user?->assignRole( 'super-utilisateur' );
        }
}
like image 1
Jeffrey Nicholson Carré Avatar answered Nov 12 '22 16:11

Jeffrey Nicholson Carré