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
1 |
composer create-project --prefer-dist laravel/laravel blog |
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.
1 2 3 4 5 6 |
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 |
Step 3: Run Migration Command
In this step we will run following command to migrate database schema.
1 |
php artisan migrate |
Step 4: Create a Model & Migration File
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Notes -m |
Let’s go to the app/Notes.php and put the below code in your files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?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'); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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 ]; }); |
Step 6: Generate Fake Data Using Tinker
Please use the following command to generate fake data using tinker:
1 |
php artisan tinker |
Now, run following command to generate 100 rows of random Notes.
1 |
factory(App\Note::class,100)->create(); |
Step 7: Generate Fake Data Using Seeder
Let’s create seeder for Note using below artisan command.
1 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { $this->call(NoteTableSeeder::class); } } |
Now, run the following command to generate 100 fake rows in the Note table.
1 |
php artisan db:seed |
Please check the database now for test data.