A seeder is a special class used to generate and insert sample data (seeds) into a database. This is an important feature in development environments, as it allows you to recreate the application with a fresh database, using sample values that you would otherwise have to manually enter each time the database was recreated.
To create a seeder, you will need to use the Vinci Console:
php vinci create:seeder UserSeeder
The command will generate a class similar to this:
<?php
use Solital\Core\Database\Seeds\Seeder;
class UserSeed extends Seeder
{
/**
* Run a Seed
*/
public function run()
{
// ...
}
}
All code you create must be inside the run()
method.
You can call other seeders within a seeder. For that use the call()
method.
<?php
use Solital\Core\Database\Seeds\Seeder;
class UserSeed extends Seeder
{
/**
* Run a Seed
*/
public function run()
{
$this->call(UserSecondSeed::class);
$this->call(UserThirdSeed::class);
}
}
Or use an array:
public function run()
{
$this->call([
UserSecondSeed::class,
UserThirdSeed::class
]);
}
To run the created Seeders, run the command:
php vinci seeder
The previous command runs all seeders. To run a specific seeder, use the --class
option.
php vinci seeder --class=UserSeeder