Laravel 7/6 Generate Fake Data Using Faker Example

In this tutorial you will learn about the Laravel 7/6 Generate Fake Data Using Faker Example and its application with practical example.

In this Laravel 7/6 Generate Fake Data Using Faker Example tutorial, I will show you how to generate fake data using laravel faker. In this tutorial you will learn to generate fake or dummy data in a database table using faker in laravel project. While working with any web application using laravel we come situations where we need some dummy or test data to available in database. Laravel faker makes this job much easier using laravel faker we can generate fake data into the database table.

Laravel 7/6 Generate Fake Data Using Faker Example

In this tutorial, you will learn how to generate fake data into the database tables using laravel faker. Please follow the instruction give below:

  • Step 1: Download Fresh Laravel Setup
  • Step 2: Setup Database Credential
  • Step 3: Run Migration Command
  • Step 4: Create a Model & Migration File
  • Step 5: Create New Factory File
  • Step 6: Generate Fake Data Using Tinker
  • Step 7: Generate Fake Data Using Seeder

Step 1: Download Fresh Laravel Setup

First of all we need to create a fresh laravel project, download and install Laravel using the below command

Step 2: Setup Database Credential

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Step 3: Run Migration Command

In this step we will run following command to migrate database schema.

Step 4: Create a Model & Migration File

Now, in this step we will create model and migration file. Please run the following command:

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

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

Step 5: Create New Factory File

In this step, we will create a factory file. 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.

Step 6: Generate Fake Data Using Tinker

Please use the following command to generate fake data using tinker:

Now, run following command to generate 100 rows of random Notes.

Step 7: Generate Fake Data Using Seeder

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

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

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

DatabaseSeeder.php

Now, run the following command to generate 100 fake rows in the Note table.

Please check the database now for test data.

In this tutorial we have learn about the Laravel 7/6 Generate Fake Data Using Faker Example and its application with practical example. I hope you will like this tutorial.