Database: Seeding
Database seeding allows you to populate your database with test or initial data. This is particularly useful for setting up development environments or deploying applications with default data.
Introduction
Seeders are classes that contain logic to insert data into your database. They are typically located in the backend/App/Seeders directory. A seeder class must extend Ecommerce\Core\Database\Seeder and implement a run method.
The core Seeder class provides methods to call other seeders, allowing you to organize your seeding logic into manageable chunks. The DatabaseSeeder class is the main entry point for seeding your application.
Seeder Methods
The Seeder class provides essential methods for managing the seeding process.
Call Additional Seeders
Call other seeder classes to split seeding logic into multiple files.
Description:
The call() method allows you to execute other seeder classes from within a seeder. This acts as a way to organize your database seeding into separate files (e.g., ProductSeeder, OrderSeeder) and call them all from a central DatabaseSeeder class. The method ensures that each seeder is resolved from the service container (allowing dependency injection) and run exactly once per seeding command, even if called multiple times.
Method Signature:
public function call(string|array $class): Seeder
Parameters:
$class- A single seeder class name or an array of seeder class names to execute.
Examples:
// In DatabaseSeeder.php
// Call a single seeder
public function run(): void
{
$this->call(UserSeeder::class);
}
// Call multiple seeders
public function run(): void
{
$this->call([
UserSeeder::class,
ProductSeeder::class,
OrderSeeder::class,
]);
}
// Nested calling: In ProductSeeder, you might call dependency seeders
public function run(): void
{
// Ensure categories exist before creating products
$this->call(CategorySeeder::class);
// Create products...
Product::create([...]);
}
SQL Output:
None (Manages PHP class execution).
The Run Method
The main entry point for a seeder class.
Description:
The run() method is where you place the logic for populating your database. This method is called when the seeder is executed. Within this method, you can use Model factories (if available), direct Model creation, or the DB facade to insert data.
Method Signature:
public function run(): void
Examples:
// Basic implementation in a custom seeder
use Ecommerce\App\Models\Product;
use Ecommerce\Core\Database\Seeder;
class ProductSeeder extends Seeder
{
public function run(): void
{
Product::create([
'name' => 'T-Shirt',
'price' => 19.99,
'stock' => 100
]);
}
}
SQL Output:
Depends on the queries executed within the method.
Seeding Data
You can use Models or the Query Builder to insert data within your seeders.
Using Models
Create records using Eloquent-like Models.
Description:
Using Models is the most expressive, readable, and maintainable way to seed data. It ensures that any model events are fired and mutators are applied.
Examples:
use Ecommerce\App\Models\Product;
use Ecommerce\App\Models\Order;
use Ecommerce\App\Models\Customer;
// Create a single product
Product::create([
'name' => 'Premium Leather Bag',
'sku' => 'BAG-001',
'price' => 149.99,
'description' => 'Handcrafted leather bag.',
'status' => 'active'
]);
// Create multiple orders using create_many (if relationship exists)
// or simple loop
$customer = Customer::find(1);
$customer->orders()->create_many([
['total' => 50.00, 'status' => 'pending'],
['total' => 120.00, 'status' => 'completed'],
]);
SQL Output:
INSERT INTO `droip_ecommerce_products` (`name`, `sku`, `price`, `description`, `status`)
VALUES ('Premium Leather Bag', 'BAG-001', 149.99, 'Handcrafted leather bag.', 'active')
INSERT INTO `droip_ecommerce_orders` (`customer_id`, `total`, `status`)
VALUES (1, 50.00, 'pending')
INSERT INTO `droip_ecommerce_orders` (`customer_id`, `total`, `status`)
VALUES (1, 120.00, 'completed')
Using Query Builder
Insert data directly using the DB facade.
Description:
For simple inserts where you don't need Model overhead or for tables without Models (like pivot tables), you can use the DB facade.
Examples:
use Ecommerce\Supports\Facades\DB;
// Insert a single record
DB::table('currencies')->insert([
'code' => 'USD',
'symbol' => '$',
'rate' => 1.00
]);
// Insert multiple records
DB::table('categories')->insert([
['name' => 'Electronics', 'slug' => 'electronics'],
['name' => 'Fashion', 'slug' => 'fashion'],
['name' => 'Home', 'slug' => 'home'],
]);
SQL Output:
INSERT INTO `droip_ecommerce_currencies` (`code`, `symbol`, `rate`)
VALUES ('USD', '$', 1.00)
INSERT INTO `droip_ecommerce_categories` (`name`, `slug`)
VALUES ('Electronics', 'electronics'), ('Fashion', 'fashion'), ('Home', 'home')
Running Seeders
Execute your seeders using the CLI commands.
Run All Seeders
Execute the DatabaseSeeder class.
Command:
wp droip db:seed
Description:
This command runs the Ecommerce\App\Seeders\DatabaseSeeder class, which in turn should call all other named seeders.
Run Specific Seeder
Execute a specific seeder class.
Command:
wp droip db:seed --class=ProductSeeder
Description:
Use the --class option to specify a particular seeder class to run. This is useful for testing a specific seeder or adding data for a specific feature.