Use Faker (Fake Data) in Laravel

Developers need fake data for testing purpose. Faker is a smart package to solve this issue. Personally, I like this package. In this article, I’ll show you how to use Faker in Laravel. So, let’s follow these steps:

Table of Contents

  1. Install Laravel and Basic Configurations
  2. Create Model and Migration
  3. Install Faker Package
  4. Create a Seeder
  5. Run the Seeder

Step 1 : Install Laravel and Basic Configurations

Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.

Step 2 : Create Model and Migration

We need a model and migration file. Run this command to create a model and migration:

php artisan make:model Post -m

-m flag defines for a model with migration. Look at the console output:

Go to database>migrations and open create_posts_table.php & paste this code:

create_posts_table.php
<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->longText('description');
            $table->string('author');
            $table->timestamps();
        });
    }

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

Now migrate the table by typing this command:

php artisan migrate

Step 3 : Install Faker Package

Let’s install the magic package by entering this command:

composer require fzaninotto/faker

Step 4 : Create a Seeder

To insert demo data to database, we need a seeder. Run the following artisan command to create a seeder:

php artisan make:seeder PostsTableSeeder

Go to database > seeds and open PostsTableSeeder.php & paste this code:

PostsTableSeeder.php
<?php

use Illuminate\Database\Seeder;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker\Factory::create();

        $limit = 10;

        for ($i = 0; $i < $limit; $i++) {
            DB::table('posts')->insert([
                'title' => $faker->sentence($nbWords = 6, $variableNbWords = true),
                'description' => $faker->paragraph($nbSentences = 3, $variableNbSentences = true),
                'author' => $faker->name,
                'created_at' => \Carbon\Carbon::now(),
                'Updated_at' => \Carbon\Carbon::now(),
            ]);
        }
    }
}

Visit the GitHub repository to get many types: https://github.com/fzaninotto/Faker

Step 5 : Run the Seeder

We are almost done. Now run the seeder by this artisan command:

php artisan db:seed --class=PostsTableSeeder

Open phpMyAdmin and check the posts table. Here’s my result:

Thank you for reading this article.