Laravel 6 Faker – Generate Fake Data Using Faker Example

Laravel 6 Faker – Generate Fake Data Using Faker Example

Overview

In this laravel faker tutorial, we would like to share with you how to generate fake data using laravel faker. We will show you two ways to generate fake or dummy data in a database table.

If we work on a large application, we work on various modules. At that time we need fake data. At that time we need to use laravel faker to generate fake data into the database table.

In this tutorial, you will learn how to generate fake data into the database tables using laravel faker from scratch tutorial with example

Generate Fake Data Using Faker

Table of Contents

  • Download Fresh Laravel Setup
  • Setup Database Credential
  • Run Migration Command
  • Create a Model & Migration File
  • Create New Factory File
  • Generate Fake Data Using Tinker
  • Generate Fake Data Using Seeder
  • Conclusion

Download Fresh Laravel Setup

In this step, we need to download the fresh laravel project setup using the command line. See the below command.

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

Setup Database Credential

After successfully install the latest version of laravel app, Go to your project .env file and set up database credential :

 DB_CONNECTION=mysql 
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here

Run Migration Command

Next, you need to run the PHP artisan migrate command on the command line. Now, go to your command-line interface tool and type the below-given command there:

php artisan migrate

Above artisan migrate command will create default database tables using migrations.

Create a Model & Migration File

In this step, we need to create a model and it’s migration file for generating fake data into the database table. Use the below command, it will create a migration file and model. The magic is in -m parameter.

 php artisan make:model Notes -m

And the result is that we have two files generated:

  1. app/Note.php
  2. database/migrations/2019_02_19_012129_create_notes_table.php

Let’s go to the app/Notes.php and put the below code in your files.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'description',
    ];
}

Next, go to database/migrations/2019_02_19_012129_create_notes_table.php and replace the below code into your file.

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNotesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('notes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('notes');
    }
}

Create New Factory File

In this step, we need to create a migration factory file. Let’s go-to app/database/factories and inside this folder, we need to create a new file name NoteFactory.php and put the below code into your file.

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Note;
use Illuminate\Support\Str;
use Faker\Generator as Faker;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$factory->define(Note::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence($nbWords = 6, $variableNbWords = true),  // Random task title
        'description' => $faker->text(), // Random task description
    ];
});

Generate Fake Data Using Tinker

In this step, Let’s open the command-line tool and run below command to generate fake data into database table notes

php artisan tinker

Next, run below command to generate 100 rows of random Notes.

factory(App\Note::class,100)->create();

Generate Fake Data Using Seeder

This is next method to generate fake data in database table notes using seeder.

Let’s create seeder for Note using below artisan command.

php artisan make:seeder NoteTableSeeder

The above command will generate NoteTableSeeder.php file in the database/seeds directory. Open NoteTableSeeder.php and update code shown as below.

<?php
use Illuminate\Database\Seeder;
class NoteTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Note::class, 100)->create();
    }
}

Next step, we will declare NoteTableSeeder inside the DatabaseSeeder. The file is DatabaseSeeder located app/database/seeds.

DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(NoteTableSeeder::class);
    }
}

Next, we need to run below the PHP artisan command to generate 100 fake rows in the Note database table.

php artisan db:seed

Let’s open your database and check the notes.

Conclusion

In this tutorial, you have learned two methods for generating fake data using the laravel faker tinker and seeder method.

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

Your email address will not be published. Required fields are marked *