How to generate faker data in laravel using seeder

How to generate faker data in laravel using seeder

Faker is way to generate dummay data in laravel and we can store into database. It helps developer to save their time by putting data one by one in database. We can get multuple records of data by running one command.

How to generate faker data in laravel using seeder, Some types of data is listed which we generate by faker.

  • Base

  • Lorem Ipsum Text

  • Person name

  • Address

  • Phone Number

  • Company name

  • Real Text

  • Date and Time

  • User Agent

  • Color

  • File

  • Image

  • Uuid

  • Barcode

  • Miscellaneous

  • Biased

  • Html Lorem

  • Unique database

  • Email

  • City name

  • Postal Code

How to generate faker data in laravel 9

Following steps are required to generate faker data in laravel 9

Step 01: Laravel Project Creating

Step 02: View Files Creating

Step 03: Student Model Creating

Step 04: Student Migration Creating

Step 05: Student Controller Creating

Setp 06: Student Seeder Creating

Step 07: Routes Creating

Step 08: Project Testing



Step 01: Laravel Project Creating

First we need a laravel fresh project to generate faker data in laravel 9. Run the given command to make a new project:

composer craete-proejct laravel/laravel StudentData



Step 02: View Files Creating

So in the next step we need view files to show students record which are generated by faker in laravel 9. Open your project and visit resouces/view Student_list and paste the given code in it:

Student.blade.php

<!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>How to generate Faker data in laravel 9</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <div class="row mt-3 pt-3">
            <div class="col-md-12">
             <h2 class="text-center mb-3">How to generate Faker Data in laravel 9</h2>
                <div class="card">
                    <div class="card-header text-center bg-info">
                        How to generate Faker data in laravel 9
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <th>Id</th>
                                    <th>Name</th>
                                    <th>Email</th>
                                    <th>Phone</th>
                                    <th>Bio</th>
                                    <th>Course</th>
                                    <th>University</th>
                                    <th>Address</th>
                                    <th>City</th>
                                    <th>Postal Code</th>
                                </thead>
                                <tbody>
                                    @foreach ($student as $student_info)
                                    <tr>
                                        <td>{{ $student_info->id}}</td>
                                        <td>{{ $student_info->name}}</td>
                                        <td>{{ $student_info->email}}</td>
                                        <td>{{ $student_info->phonenumber}}</td>
                                        <td>{{ $student_info->bio}}</td>
                                        <td>{{ $student_info->course}}</td>
                                        <td>{{ $student_info->university}}</td>
                                        <td>{{ $student_info->address}}</td>
                                        <td>{{ $student_info->city}}</td>
                                        <td>{{ $student_info->postal_code}}</td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="   https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Step 03: Student Model Creating

So run the given command to make student model.

Php artisan make:model Student

Step 04: Student Migration Creating

After creating of model we need a migration files which use to create a table in database. We will define columns in it. Run the given command to make migration and update with given code.

Php artisan make:migration create_students_table

so visite database/migrations and open create_students_table files and update:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

    public function up()

    {

        Schema::create('students', function (Blueprint $table) {

            $table->id();

            $table->string('name');

            $table->string('email');

            $table->string('phonenumber');

            $table->string('image');

            $table->text('bio');

            $table->string('course');

            $table->string('university');

            $table->string('address');

            $table->string('city');

            $table->string('postal_code');

            $table->timestamps();

        });

    }

    public function down()

    {

        Schema::dropIfExists('students');

    }

};

 

Step 05: Student Controller Creating

Here we need a controller which we use to show the data which is stored into database with the help of seeder. Run the given command to create StudentController.

Php artisan make:controller StudentController

After controller creating open App/Http/controllers and open StudentController and then update with given code:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller
{
    public function index(){

      $student = Student::all();

      return view('student_list')->with('student', $student);

    }

}

Setp 06: Student Seeder Creating

In this tutorial main part is seeder which we use to make fake records and store into database. We will create multiple records by running a single command also we create a single records. Its depend on you how much records do you want. Run the given command to create StudentSeeder.

Php artisan make:seeder StudentSeeder

After StudentSeeder creating visit Database/Seeders and open StudentSeeder and update with given code:

<?php

namespace Database\Seeders;

use Faker\Factory as faker;

use App\Models\Student;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;

use Illuminate\Database\Seeder;

class StudentSeeder extends Seeder

{

    public function run()

    {

        $faker = Faker::create();

        for($i=0; $i<=10;$i++){

            $student = new Student;

            $student->name = $faker->name;

            $student->email = $faker->email;

            $student->phonenumber = $faker->phoneNumber;

            $student->image = $faker->imageUrl;

            $student->bio = $faker->sentence;

            $student->course = $faker->word;

            $student->university = $faker->sentence;

            $student->address = $faker->address;

            $student->city = $faker->city;

            $student->postal_code = $faker->postcode;

            $student->save();

    }

    }

}

After that open DatabaseSeeder and update with given code:

 

public function run()

    {

        $this->call([

            StudentSeeder::class

        ]);

    }

So generate records storing into database run the given command:

php artisan db:seed

Step 07: Routes Creating

Visit Routes/web.php and update with given code:

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\StudentController;

Route::get('/', [StudentController::class, 'index']);

 



Step 08: Project Testing

Its time to test our project. So run the given command to start server:

php artisan serve

Click here to open your project: http://localhost:8000/



Comments

No Comment posted Yet!

Leave a Reply

OK! You can skip this field.