1. Creating a Factory
Step 1: Generate the Factory
Run the following command to create a factory for your model:
php artisan make:factory ModelNameFactory --model=ModelName
Example:
php artisan make:factory DepartmentFactory --model=Department
This will create a file at database/factories/DepartmentFactory.php
.
Step 2: Define the Factory
Open the factory file and define the data structure. Use realistic data for better testing.
namespace Database\Factories;
use App\Models\Department;
use Illuminate\Database\Eloquent\Factories\Factory;
class DepartmentFactory extends Factory
{
protected $model = Department::class;
public function definition()
{
return [
'name' => $this->faker->word, // Generates a random department name
'description' => $this->faker->sentence, // Short description
'created_at' => now(),
'updated_at' => now(),
];
}
}
2. Creating a Seeder
Step 1: Generate the Seeder
Run the following command to create a seeder:
php artisan make:seeder SeederName
Example:
php artisan make:seeder DepartmentSeeder
This will create a file at database/seeders/DepartmentSeeder.php
.
Step 2: Define the Seeder
In the seeder file, use the factory to generate records.
namespace Database\Seeders;
use App\Models\Department;
use Illuminate\Database\Seeder;
class DepartmentSeeder extends Seeder
{
public function run()
{
// Generate 10 departments
Department::factory(10)->create();
}
}
3. Register the Seeder
Step 1: Add Seeder to DatabaseSeeder
In database/seeders/DatabaseSeeder.php
, call the DepartmentSeeder
:
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
DepartmentSeeder::class,
]);
}
}
4. Run the Seeder
Run the following command to seed your database:
php artisan db:seed
To run a specific seeder:
php artisan db:seed --class=DepartmentSeeder
5. Professional Practices
- Use Relationships in Factories If your model has relationships, define them in the factory:
'user_id' => User::factory(), // Generates a user and assigns it to this model
- Use Custom States Create states for specific variations of the model:
public function admin()
{
return $this->state([
'role' => 'admin',
]);
}
Use it in seeders:
Department::factory()->admin()->create();
- Organize Seeders For large projects, organize seeders into modules:
database/
└── seeders/
├── Admin/
│ ├── UserSeeder.php
│ └── RoleSeeder.php
├── Product/
│ └── ProductSeeder.php
└── DepartmentSeeder.php
- Testing with Factories Use factories in tests to generate test data:
$department = Department::factory()->create();
$this->assertDatabaseHas('departments', ['id' => $department->id]);
- Clear and Seed For fresh testing, run:
php artisan migrate:fresh --seed
6. Example Usage
If you have a Department
model, you can now generate:
- Random departments:
Department::factory()->count(10)->create();
- Custom departments:
Department::factory()->create([
'name' => 'Human Resources',
]);
Top comments (0)