How to use Faker with Laravel 5.7 Tutorial

Generic placeholder image
Brijpal Sharma
laravel  

Date:-December 16th, 2018 , Posted On:- Web Development


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

Now, you can check your database.

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

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.


About the author

user profile
Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...

1 comments
Generic placeholder image
akash sharma
August 08th, 2019 - 10:18AM

yeah!! nice blog, I like to read these types of blogs on laravel. after the launch of 5.8 versions, many new features have come right now waiting for 6.0 launched new features...

You must be logged in to post a comment.