How to Insert Fake or Dummy Data in Laravel

How to Insert Fake or Dummy Data in Laravel

In this tutorial, we are going to learn how to generate dummy data in laravel using seeders and model factories. These are the common ways of generating dummy data into laravel

Laravel factory has really made it so easy to work on dummy data using faker.

Follow below steps :

  1. First, we need to create a seed file inside database/seeders directory
  2. We must register seeder file inside databaseSeeder.php
  3. In terminal run php artisan make:seeder UserSeeder command

It will create a UserSeeder.php file under database/seeders  . Register this seeder into the DatabaseSeeder.php inside database/seeder .

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UserSeeder::class);
    }
}

Then inside  UserSeeder.php add code like below    

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(Faker $faker)
    {
        for ($i = 0; $i < 5; $i++) {
            DB::table('users')->insert([
                'name' => $faker->name,
                'email' => $faker->unique()->safeEmail,
                            'password' => bcrypt('pass123@11'), // you Can also used Hash::make('pass123@11')
                            'email_verified_at' => now(),
                            'remember_token' => Str::random(10)
                   ]);
        }
        

    }
}

Now go to your terminal and run php artisan db:seed command It will run all your seeder then check your DB

Insert Fake data Using Model Factories

Using terminal run factory command

php artisan make:factory UserFactory --model=User

It will create a file UserFactory.php inside your database/factories. Edit it with below code

<?php

namespace Database\Factories;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name(),
            'email' => $this->faker->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password' => Hash::make('password'),
            'remember_token' => Str::random(10),
        ];
    }
}

Create a new Seeder

php artisan make:seed UserTableSeeder
<?php


namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::factory()->count(10)->create();
    }
}

Now run below command and check DB

php artisan db:seed --class=UserTableSeeder

Leave a Reply

Your email address will not be published. Required fields are marked *