Generating dummy data with faker in laravel

Introduction

Faker is a PHP library that generates fake data. We can use it extensively while testing our laravel application. We can easily populate our database with fake data. After that, we can run the necessary tests for our requirements.

Setup

Faker package comes along with laravel installation. You can see the composer.json file in your root directory. You can see require-dev something like this,

"require-dev": { 
    "filp/whoops": "~2.0", 
    "fzaninotto/faker": "~1.4", 
    "mockery/mockery": "~1.0", 
    "phpunit/phpunit": "~6.0", 
    "symfony/thanks": "^1.0" 
},

fzaninotto/faker is the package we are using to generate fake data. If there is no faker package included in composer.json then, you can install it by running following command.

composer require fzaninotto/faker

Schema for database table

Now, we will create a simple table using laravel migration. Let's create a articles table migration like this,

php artisan make:migration create_articles_table --create=articles

Above command will generate a migration file 2018_07_14_031333_create_articles_table.php inside database/migrations/ directory. We will add title and body columns to the articles table.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

Although ID and Timestamps column (created_at,updated_at) are created by default,We have to add title and body column.

Also Read: Specific Table Migration In Laravel

In order to create articles table, we have to run the following command.

php artisan migrate

Hence, we have articles table in the database. Now, we will use faker to populate the articles table in the database.

Using Factory

Let's create a factory where we will add code to insert fake data to our database.

php artisan make:factory ArticleFactory

The above command will generate a file ArticleFactory.php inside database/factories/ directory.

use Faker\Generator as Faker;

$factory->define(Model::class, function (Faker $faker) {
    return [
        //
    ];
});

In order to make above factory to work, we have to change default Model::class to our article model and return fake title and body. Our code will be,

use Faker\Generator as Faker;

$factory->define(App\Article::class, function (Faker $faker) {
    return [
        'title' => $faker->text(50),
        'body' => $faker->text(200)
    ];
});

Here, title will be a text with 50 characters and body will be a text with 200 character. In order to find more details about available options, you can see in following link.

Faker Formatters

Now, Our faker is ready and we need a seeder file to insert fake data.

Create Seeder and Register to Database Seeder

Let's create articles table seeder with following command.

php artisan make:seeder ArticlesTableSeeder

Above code will generate a seeder class ArticlesTableSeeder.php inside database/seeders/ directory.

<?php

use Illuminate\Database\Seeder;

class ArticlesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
    }
}

Now, we will add some code inside run and register the seeder to main database seeder.

<?php 

use Illuminate\Database\Seeder;

class ArticlesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Article::class, 30)->create();
    }
}

To register the seeder, add following lines of code to DatabaseSeeder.php

So, your Database Seeder should look like following,

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
         $this->call(ArticleTableSeeder::class);
    }
}

Now, In order to run the seeder file, execute following console command.

php artisan db:seed

In this way, we can create a faker for a eloquent model so that we can populate the database using database seeder.


share :

Sagar Gautam

A Computer Engineer from Nepal.


Leave your Feedback