Laravel 8 Generate Fake Data Using Faker Example

Sometimes you are testing project in localhost, you will also need data to do proper testing. For that, you will need to import database from the running site or need to insert data manually. This is not the proper way of making testing. Or you can generate fake data programmatically. This is where Faker is needed.

Faker is a PHP package that generates dummy data for testing. With Faker you can generate mass amount of testing data as you needed. Faker comes preinstalled in Laravel framework. You can also use Faker in other frameworks or your own native PHP websites.

In this article, we will create Laravel project and generate dummy data for Article. So let’s start.

Project setup

First of all generate latest Laravel project with bellow command in your Terminal or CMD.

composer create-project laravel/laravel faker

This will create project in the existing directory. Import the project in your text editor and setup your database in .env file in the root of your Laravel project.

As we have earlier said, Faker comes preinstalled in Laravel, so we don’t need to install Faker package in Laravel.

Now let’s create database migration using belllow artisan command:

php artisan make:migration create_articles_table

Now add table fields in the migration class in the up() method.

<?php

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

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title', 255);
            $table->integer('user_id');
            $table->string('slug', 255);
            $table->string('keywords', 255);
            $table->string('description', 255);
            $table->text('content');
            $table->timestamps();
        });
    }

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

Run the migrate command to generate table in the database:

php artisan migrate

We will also need to generate Model class. So create model using command.

php artisan make:model Article

Generating Factory class

That was our project setup. Now let’s start to work on generating dummy data. For that, first create bellow Laravel artisan command:

php artisan make:factory ArticleFactory --model=Article

It will generate factory class at database/factories/ArticleFactory.php file. Open that file and add fields in the 'return' array as bellow.

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Article;
use Faker\Generator as Faker;

$factory->define(Article::class, function (Faker $faker) {
    return [
        'title' => $faker->text,
        'user_id' => factory(App\User::class),
        'slug' => $faker->slug,
        'keywords' => $faker->text,
        'description' => $faker->text,
        'content' => $faker->paragraph,
    ];
});

Check this official documentation for the property of $faker object to generate data according to your table fields.

Now open database/seeds/DatabaseSeeder.php file and add the bellow lines in the run() function. This will call factory class and will generate dummy data in the database tables.

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        factory(App\User::class, 50)->create();
        factory(App\Article::class, 200)->create();
    }
}

This will generate 50 records for the users table and 200 records for the articles table.

Factory class for users table is already generated in Laravel bydefault at database/factories/UserFactory.php. You may want to modify fields according to your users table.

Conclusion

In this way, you can generate dummy data in your localhost website and make proper testing.