Hello Devs,

In this tutorial, we are going to learn how to generate fake data using faker in laravel application.

Follow this step by step guide given below:



Step 1 : 


Install Laravel App

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



Step 2 : 


Setup Database Configuration open ".env" file and change the database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password



Step 3 : 


Create Table Migration and Model

php artisan make:model Blog -m

/database/migrations/2020_05_27_100722_create_blogs_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBlogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->longText('description');
            $table->string('auther_name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('blogs');
    }
}


Run this command:

php artisan migrate


app/Blog.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
    protected $fillable = ['title','description','auther_name'];
}



Step 4 : 


Install Faker Package

composer require fzaninotto/faker



Step 5 : 


Create Blog Seeder

php artisan make:seeder BlogSeeder

database/seeds/BlogSeeder.php

<?php

use Illuminate\Database\Seeder;
use App\Blog;

class BlogSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker\Factory::create();

        $limit = 10;

        for ($i = 0; $i < $limit; $i++) {
            Blog::create([
                'name' => $faker->sentence($nbWords = 6, $variableNbWords = true),
                'auther_name' => $faker->name,
                'description' => $faker->paragraph($nbSentences = 3, $variableNbSentences = true),
                'created_at' => \Carbon\Carbon::now(),
                'updated_at' => \Carbon\Carbon::now(),
            ]);
        }
    }
}



Step 6 : 


Create Route

routes/web.php

Route::get('fake-blog', 'BlogController@fakeBlogs');



Step 7 :

 

Create Controller

php artisan make:controller BlogController

app/http/controller/BlogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Blog;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function fakeBlogs()
    {
        $blogs = blog::get();
        return view('fakeData',compact('blogs'));
    }

}



Step 8 : 


Create Blade File /resources/views/fakeData.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Generate Fake Data Using Faker in Laravel - Rathorji.in</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha256-aAr2Zpq8MZ+YA/D6JtRD3xtrwpEz2IqOS+pWD/7XKIw=" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha256-OFRAJNoaD8L3Br5lglV7VyLRf0itmoBzWUoM+Sji4/8=" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-8 offset-2">
                <div class="card mt-5">
                    <div class="card-heading bg-info">
                        <h3 class="text-center text-white p-3"><strong>Generate Fake Data Using Faker in Laravel - Rathorji.in</strong></h3>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>No.</th>
                                        <th>Name</th>
                                        <th>Author Name</th>
                                        <th>Description</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($blogs as $blog)
                                        <tr>
                                            <td>{{ $blog->id }}</td>
                                            <td>{{ $blog->title }}</td>
                                            <td>{{ $blog->auther_name }}</td>
                                            <td>{{ \Str::limit($blog->description,50) }}</td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Run this command:

php artisan serve

Open this URL:

http://localhost:8000/fake-blog


I hope this example helps you.