In the process of working, I have encountered many cases of updating data for a table using seeder . So how can we add custom parameters when running seeder ?.
1. Passing parameters directly in the command at runtime
1.1 Using command->ask('key')
In the seeder file use the ask('SEED_KEY', 'default_value')
method, this command will ask to enter the value when running the seeder if you leave it blank and enter the SEED_KEY
value will be default_value
.
// database/seeders/UpdateSomeThingSeeder.php
class UpdateSomeThingSeeder extends Seeder
{
public function run ( )
{
$valueName = $this -> command -> ask ( 'SEED_KEY' , 'Thanh' ) ;
$this -> command -> alert ( "User name: { $userName } " ) ;
}
}
Test run the seeder
php artisan db:seed --class=UpdateSomeThingSeeder
This is the result when after running the seeder
$php artisan db:seed --class=UpdateSomeThingSeeder
User name [Thanh]:
> Cong Thanh
*********************************
* User name: Cong Thanh *
*********************************
Database seeding completed successfully.
1.2 Using env('key')
In the seeder file use env('SEED_KEY', 'default_value')
, this command will get the SEED_KEY
variable value in the .env
configuration file or you can also pass it directly when running the seeder
// database/seeders/UpdateSomeThingSeeder.php
class UpdateSomeThingSeeder extends Seeder
{
public function run ( )
{
$valueName = env ( 'SEED_KEY' , 'Thanh' ) ;
$this -> command -> alert ( "User name: { $userName } " ) ;
}
}
Test run the seeder
SEED_KEY='Cong Thanh' php artisan db:seed --class=UpdateSomeThingSeeder
This is the result when running the seeder
$SEED_KEY='Cong Thanh' php artisan db:seed --class=UpdateSomeThingSeeder
*********************************
* User name: Cong Thanh *
*********************************
Database seeding completed successfully.
2. Passing parameters between seeders
In the seeder file use callWith(seederClass, params = [])
this function has 2 parameters:
- seederClass: similar to the
call()
function, this parameter is theClass
of the seeder you want to run - params: variables you want to pass to the seeder
seederClass
at runtime
// database/seeders/DatabaseSeeder.php
class DatabaseSeeder extends Seeder
{
public function run ( )
{
$this -> callWith ( UpdateSomeThingSeeder :: class , [ 'userName' => 'Cong Thanh' ] ) ;
}
}
// database/seeders/UpdateSomeThingSeeder.php
class UpdateSomeThingSeeder extends Seeder
{
public function run ( $userName = 'Thanh' )
{
$this -> command -> alert ( "User name: { $userName } " ) ;
}
}
Test run the seeder
php artisan db:seed
This is the result when running the seeder
php artisan db:seed
Seeding: DatabaseSeedersUpdateSomeThingSeeder
*********************************
* User name: Cong Thanh *
*********************************
Seeded: DatabaseSeedersUpdateSomeThingSeeder (0.35ms)
Database seeding completed successfully.
3. Reference
4. Conclusion
Hope this sharing will help you newbie a bit in the process of learning about Laravel. If you find it useful, please give me 1 vote 👍 to let more people know and share this.
I’m Cong Thanh, thank you for following my article, if you have any questions, please comment below.