Seeders
After creating your database schema using Migrations, you will usually need to populate your tables with initial data so you can start working on your application. This is where Seeders come into play.
A seeder is a class responsible for inserting data into database tables. Seeders are commonly used for tasks such as creating roles and permissions, defining product categories, or inserting initial users that can log into the system.
Creating new Seeder
To create a new seeder file, run the following command in your terminal. Note that you do not need to include the word "Seeder" in the name, as it will be automatically appended by SigmaPHP.
./bin/sigma-cli create:seeder Products
This command will generate a new file called ProductsSeeder.php inside the default seeders directory:
database/seeders
If you want to customize the location where seeder files are stored, you can change the path in the
database.php configuration file. For more details, please refer to the Configurations section.
/**
* Default path for seeders.
*/
'path_to_seeders' => env('PATH_TO_SEEDERS', '/database/seeders'),
After opening the generated seeder file, you will notice the default template which contains the run()
method. All seeding logic should be written inside this method.
<?php
use SigmaPHP\DB\Seeders\Seeder;
class ProductsSeeder extends Seeder
{
/**
* @return void
*/
public function run()
{
// your instructions
}
}
Running Seeders
Once your seeders are ready, you can execute them using the SigmaPHP CLI tool.
To run all seeders:
// run all seeders
./bin/sigma-cli seed
You can also execute a specific seeder by providing its class name:
// run specific seeder
./bin/sigma-cli seed ProductsSeeder
Available Methods
Insert Data
The insert() method allows you to insert one or multiple rows into a database table.
<?php
use SigmaPHP\DB\Seeders\Seeder;
class ProductsSeeder extends Seeder
{
/**
* @return void
*/
public function run()
{
$this->insert(
'products',
[
['name' => 'Cell Phone', 'price' => 100.00],
['name' => 'Laptop', 'price' => 500.00],
['name' => 'TV', 'price' => 1000.00],
]
);
}
}
Update Data
The update() method updates existing rows in a table based on a given condition.
<?php
use SigmaPHP\DB\Seeders\Seeder;
class ProductsSeeder extends Seeder
{
/**
* @return void
*/
public function run()
{
$this->update(
'products',
['price' => 200.00], // update values
['name' => 'Cell Phone'] // search condition
);
}
}
Delete Data
The delete() method removes records from a table based on a specified condition.
<?php
use SigmaPHP\DB\Seeders\Seeder;
class ProductsSeeder extends Seeder
{
/**
* @return void
*/
public function run()
{
$this->delete(
'products',
['name' => 'Cell Phone'] // search condition
);
}
}
Note: The condition parameter can be omitted. However, doing so will delete all records from the specified table.
Using the Query Builder
SigmaPHP-DB also provides a Query Builder that can be used directly inside seeders when more advanced database operations are required.
<?php
use SigmaPHP\DB\Seeders\Seeder;
class ProductsSeeder extends Seeder
{
/**
* @return void
*/
public function run()
{
$category = $this->queryBuilder
->table('product_categories')
->where('id', '=', 5)
->get();
$this->update(
'products',
[
'category' => $category['name']
]
);
}
}
To learn more about the query builder and its available features, please refer to the Query Builder Documentation.