DEV Community

Cover image for Laravel Factory and Seeder Implementation
Anas Hussain
Anas Hussain

Posted on

Laravel Factory and Seeder Implementation

Definitions

What is a Laravel Factory?

A Laravel Factory is a feature that allows developers to generate fake data for testing or seeding purposes. It defines a default "state" for a model using the definition() method. Factories leverage Faker to create realistic and random data that mimic real-world input.


What is a Laravel Seeder?

A Laravel Seeder is used to populate the database with dummy data or default values. It is typically used to set up initial data for development, testing, or production environments. Seeders work hand-in-hand with factories to generate large amounts of structured data.


1. Create a Factory

To generate a factory for the Designations model, use the following Artisan command:

php artisan make:factory DesignationFactory --model=Designations
Enter fullscreen mode Exit fullscreen mode

2. Create a Seeder

Generate a seeder for the Designations table:

php artisan make:seeder DesignationSeeder
Enter fullscreen mode Exit fullscreen mode

3. Update the Designations Model

Ensure the following changes are added to your Designations model file:

File: app/Models/Designations.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Designations extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'description'];
}
Enter fullscreen mode Exit fullscreen mode

4. Update the Seeder File

Modify the seeder file to use the factory for creating dummy records.

File: database/seeders/DesignationsSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Designations;

class DesignationsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        Designations::factory()
            ->count(3) // Number of records to create
            ->create();
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Update the Factory File

Define the attributes for the Designations model in the factory file.

File: database/factories/DesignationsFactory.php

<?php

namespace Database\Factories;

use App\Models\Designations;
use Illuminate\Database\Eloquent\Factories\Factory;

class DesignationsFactory extends Factory
{
    protected $model = Designations::class;

    /**
     * Define the model's default state.
     */
    public function definition(): array
    {
        return [
            'name' => fake()->name(),
            'description' => fake()->sentence(10),
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

6. Run the Seeder

Run the seeder to populate the database with test data:

php artisan db:seed --class=DesignationsSeeder
Enter fullscreen mode Exit fullscreen mode

Conclusion

By following these steps:

  1. You have created a Factory for generating dummy data.
  2. You have set up a Seeder to populate the database.
  3. Your model is properly configured to use HasFactory.

This approach ensures a clean and structured way to seed test data into your Laravel application.

Top comments (0)