Generating Fake Data In Laravel 9 | Laravel Seeding

This is another laravel tutorial for seeding fake data using the factory. We will generate dummy data using a model and seed them into db.

There is some fake data is needed for testing in the prod environment. You can enter data manually or generate using script into db.

The laravel provides an option to generate sample data in a gentle way. Laravel has a feature called model factories that allows us to generate fake data.

The libraries fzaninotto/faker help to generate fake data, we can create a lot of fake data using this libs. You do not need to install this package if you are using laravel 9 or higher. You can install faker libs using the composer command –

composer require fzaninotto/faker --dev

After successfully installing faker, we will create our user seeder class to generate user fake data.

Create Person Model and Migration

We will create a persons models factory using the following command –

php artisan make:model Person -m

The above command will create persons_table.php file into the migration table. The following code will add into up() method.

public function up()
{
    Schema::create('persons', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('firstName');
        $table->string('lastName');
        $table->timestamps();
    });
}

Now migrate using the below command –

php artisan migrate

Create a model factory for seeding data

Let’s create an PersonFactory by using the following command.

php artisan make:factory PersonFactory

The above command will create PersonFactory.php file inside the database/factories/ folder. We will add some attributes information into this file –

// PersonFactory.php

use Faker\Generator as Faker;
use App\Person;

$factory->define(person::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'firstName' => $faker->firstName,
        'lastName' => $faker->lastName
    ];
});

Whereas –

  • $faker->name: Generate fake person name data.
  • $faker->firstName : Generate fake person firstname data.
  • $faker->lastName : Generate fake person lastname data.

We have defined a factory and passed Person.php model as a first parameter. Then we have the callback function that defines the data and returns it in an array.

We will create a seeder file for the person model. This file is responsible to generate the fake records inside the database. The following command will generate PersonsTableSeeder.php file –

php artisan make:seeder PersonsTableSeeder

The file will generate into database/seeds folder. We will add the below code to create dummy data –

<?php

use Illuminate\Database\Seeder;
use App\Person;

class PersonsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $count = 100;
        factory(Person::class, $count)--->create();
    }
}

The above file will create 100 fake records in the database when you run the seeder. Now we will make PersonsTableSeeder entry into DatabaseSeeder.php file which is inside the database/ seeds folder.

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(PersonsTableSeeder::class);
    }
}

Now, we will regenerate the Composer’s autoloader using the following dump-autoload command –
composer dump-autoload

How to seed data into a database

We will seed data into a person table using the below command.

php artisan db:seed

Above command will generate 100 fake records inside the persons table.

The unit testing will cover in the next laravel tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *