How to use Faker with Laravel 5.7 Tutorial

In this blog post, we learn how to use Faker with Laravel 5.7. Laravel faker provides free fake data for SQL for testing purposes.

Today blog post topic is how to use Faker with Laravel 5.7. fake data use for the developer as a testing purpose. sometimes developer needs the bulk of data for testing for pagination or dummy data. so let’s start.

In this tutorial, we create a new Laravel 5.7 project and create a new database for inserting fake data into the database using Laravel faker.

Step:1 Create a new Laravel 5.7 Project.

You can create a fresh new Laravel project using this following command.

composer create-project --prefer-dist laravel/laravel blog

or

laravel new blog

It takes some minute or sec depends on your internet seeped after successfully install. 

Step:2 Create a new model and migration.

Run this following command to generate model and migration.

php artisan make:model Article -m

Here -m flag defines for a model with migration.

After running this command you will find a new migration file in database folder let’s add some column.

database/migration

 public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('description');
            $table->text('body');
            $table->timestamps();
        });
    }

Now, migrate table using this following this command.

php artisan migrate

After migration, you can see the blank table in the database.

Step:3 Create a Seeder for Articles table.

Now create a seeder using this following command.

php artisan make:seeder ArticlesSeederTable

You can see a new file in database/seeds  “ArticlesSeederTable.php”

In ArticlesSeederTable.php 

<?php

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;

class ArticlesSeederTable extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create('App\Article');
        DB::table('articles')->insert([
        	'title' => $faker->sentence(),
        	'description' => $faker->sentence(),
        	'body' => $faker->paragraph(),
        	'created_at' => \Carbon\Carbon::now(),
        	'Updated_at' => \Carbon\Carbon::now(),
        ]);

    }
}

After that run this following command to insert fake data into the database.

php artisan db:seed --class=ArticlesSeederTable
table after seed data

Ok, Seeder work but what we do if we need 1000 fake data in the database. so need to run a loop for insert data into the database.

So, final code for ArticlesSeederTable.

<?php

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;

class ArticlesSeederTable extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create('App\Article');
        
        for($i = 1 ; $i <= 10 ; $i++){
        	DB::table('articles')->insert([
        	'title' => $faker->sentence(),
        	'description' => $faker->sentence(),
        	'body' => $faker->paragraph(),
        	'created_at' => \Carbon\Carbon::now(),
        	'Updated_at' => \Carbon\Carbon::now(),
        ]);
        }

    }
}

Now, Database

Laravel table seed with data

So finally we Complete How to use Faker with Laravel 5.7 Tutorial . if you have any dought feel free to ask in comment box.