Php

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

Related Post

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.

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

3 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

4 months ago

Categories