How to use Laravel Factory and Seeder with examples

In this tutorial, you will learn how to use Laravel Factory and Seeder with examples to generate dummy data for your database. Laravel provides a quick and efficient way to create test data using factories and seeders, which is extremely helpful during development and testing. So, you do not have to spend hours creating test data manually for your project. Factory and Seeder are Laravel Libraries, using which you can create test data easily.

We will use a custom table and create test data using Laravel Factory. We will update DatabaseSeeder.php and run database seeder. Let us use the below steps to complete creating test data.

  1. Create a Laravel project
  2. Create a MySQL database migration
  3. Create a Laravel factory file for the newly created table
  4. Update DatabaseSeeder.php
  5. Run db:seed
  6. Verify data in the table

1. Create a Laravel project

We will create a project in Laravel. Make sure you have Composer and PHP installed in your system. We will name the project as lara_seeder. Run the below command from the terminal.


composer create-project --prefer-dist laravel/laravel lara_seeder

This will create a folder named "lara_seeder" under the folder where you run the above command.

2. Create a MySQL database migration

Update .env file for database details. We are using a MySQL database named 'lara_demo', so we updated the file as below:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lara_demo
DB_USERNAME=root
DB_PASSWORD=

We will create a new table called "applications" for all the registered applicants. You can use an existing table like "users" to populate test data into it. Then you do not need to create a new table, you can directly create the factory file.

Let us create a Laravel migration file for this table. Go to your project folder and run the below command from the VS code terminal.


php artisan make:model Application -m

It will create the model and the migrations file for the table "applications". Check the folder "database\migrations" for the migration file.

Update the migration file

Let us update the above migration file as below:


public function up(): void
    {
        Schema::create('applications', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('address')->nullable();
            $table->timestamps();
        });
    }

Now run Laravel migration to create the default Laravel tables and our custom "applications" table. Run the below command from the terminal to create the tables.


php artisan migrate

After running the migration see the structure of 'applications' table.

Laravel factory and seeder example

3. Create a Laravel factory file for the newly created table

Create a factory for the Application model as below:


php artisan make:factory ApplicationFactory --model=Application

This will create a factory file "ApplicationFactory.php" under "database\factories".

Update ApplicationFactory as below:


public function definition(): array
    {
        return [
            'name' => $this->faker->name(),
            'email' => $this->faker->unique()->safeEmail(),
            'address' => $this->faker->address,
        ];
    }

4. Update DatabaseSeeder.php

Now, update the run() method of DatabaseSeeder.php under the "database/seeders" folder to call the factory we just created. For testing purposes, we want to build 100 rows in the "applications" table. You can change this number as needed.


public function run(): void
    {
        \App\Models\Application::factory(100)->create();
      
    }

5. Run db:seed

Now, run Laravel database seeder from the terminal to create the rows:


php artisan db:seed

6. Verify data in the database

You can see that 100 rows are created in the "applications" table.

laravel factory faker

Laravel image upload Conclusion

Using Laravel Factory and Seeder is one of the easiest ways to generate dummy data for testing and development. It saves time by automatically generating sample data with just a few commands. I hope this will be useful to you when you want to test your Laravel application during development. Please post your comments on this tutorial in the Comment section below.

Post a Comment

Save my Name and Email id for future comments