Generate Dummy data in laravel 5

Hello World, Welcome to this website. Today we will discuss how to generate dummy data in laravel 5 with faker class. Generating dummy data with the automatic process will definitely increase our productivity.

Here you will not learn just about generating dummy data but you will also learn the following things:

  • Laravel Artisan commands
  • Create controller and model with artisan command
  • Migration artisan commands
  • Blade templates

This is very simple tutorial. In this tutorial, we will generate dummy data for students with following fields.

  • Name
  • Email
  • Phone Number
  • Address
  • Gender

I have assumed that you have installed laravel 5 on your system. If not then please refer this article. We will implement all these things in following steps.

  1. Create database and configure in laravel
  2. Create controller and view for showing dummy data
  3. Create model and factory for generating dummy data
  4. Generate dummy data with tinker
  5. Show dummy data in view template
  • Create database and configure in laravel

I am using xampp server and window operating system so I will do all the things according to this environment. First of all create database in phpmyadmin and put the database detail in .env file on root folder.

  • Create controller and view for showing dummy data

Controller

We will create controller with artisan command. Go to the root folder in command line and run the following command.

php artisan make:controller StudentController

Above command will create controller file in /app/Http/Controllers/StudentController.php

Open this file and create the following method in file.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StudentController extends Controller
{
    // Get all student data
    function index(){
        return view('students');
    }
}

View

Now we will create students template. Create students.blade.php in /resources/views/students.blade.php file and add the following code in this file. And also Add the bootstrap css in /public/lib/ folder.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Generate Dummy Data with Laravel 5</title>
    {{-- Bootstrap 4 --}}
    <link rel="stylesheet" href="{{ asset('lib/bootstrap.min.css') }}">
</head>
<body>
    <div class="container mt-4">
        <div class="row">
            <div class="col-sm-12">
                <a href="http://projectsplaza.com"><img src="{{ asset('imgs/projectsplazadotcom.png') }}" /></a>
                <div class="card mt-3">
                    <div class="card-header">Students Dummy Data</div>
                    <div class="card-body">
                        <table class="table table-bordered">
                            <tr>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Phone</th>
                                <th>Address</th>
                                <th>Gender</th>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Route

  • Open /routes/web.php file and add the following code at the end of this file.
  • // Form Generating Summy Data Content
    Route::get('students','StudentController@index');

     

  • Open the project in the browser like following
  • localhost/your-laravel-folder/public/students
  • You will see the following screen

  • Create model and factory for generating dummy data

Model and Migration

  • Open command line and run the following command
  • php artisan make: model Student –m
  • Open /database/migrations/students_ migration file and add the following code in up function.
  • // Migration file code
    Schema::create('students', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('email');
                $table->string('phone');
                $table->string('address');
                $table->string('gender');
                $table->timestamps();
            });

     

  • Run the following command to create students table with the above structure.
  • php artisan migrate
  • After running the above command, you will see that new table has created.

Generate dummy data with tinker

  • Open factories/userFactory.php file and add the following code at the end of the file.
  • // Factory Code
  • Run the following commands
    • php artisan tinker
    • Factory(App\Student::class,10)->create()

Above commands will generate dummy data in database.

  • Show dummy data in view template

 

  • Open students.blade.php file and add replace with the following code.
  • <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Generate Dummy Data with Laravel 5</title>
        {{-- Bootstrap 4 --}}
        <link rel="stylesheet" href="{{ asset('lib/bootstrap.min.css') }}">
    </head>
    <body>
        <div class="container mt-4">
            <div class="row">
                <div class="col-sm-12">
                    <a href="http://projectsplaza.com"><img src="{{ asset('imgs/projectsplazadotcom.png') }}" /></a>
                    <div class="card mt-3">
                        <div class="card-header">Students Dummy Data</div>
                        <div class="card-body">
                            <table class="table table-bordered">
                                <tr>
                                    <th>Name</th>
                                    <th>Email</th>
                                    <th>Phone</th>
                                    <th>Address</th>
                                    <th>Gender</th>
                                </tr>
                                @if(count($students)>0)
                                @foreach($students as $s)
                                    <tr>
                                        <td>{{ $s->name }}</td>
                                        <td>{{ $s->email }}</td>
                                        <td>{{ $s->phone }}</td>
                                        <td>{{ $s->address }}</td>
                                        <td>{{ $s->gender }}</td>
                                    </tr>
                                @endforeach
                                @endif
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    </html>

     

  • Open StudentController file and add the following code in the index method
  • // Index method code
    // Get all student data
        function index(){
            $students=Student::all();
            return view('students',['students'=>$students]);
        }

     

  • Add the following in just before the StudentController class
  • Use App\Student

If you like this article then pleases share and comment. 🙂

Leave a Reply