# How to insert Bulk Fake Data in Laravel 9 using Factory & Seeder.

In this post, I am going to explain about creating dummy data in the database by using Laravel Eloquent Factory and Seed The Database by using Database Seeder.

## Eloquent Factory

Eloquent Factory is nothing but an image of a Model with fake data.

To create a model factory you have to run the command below.

```php
php artisan make:factory UserFactory --model=User
```

This will generate the file in database/factory/UserFactory.php

Now let's add fake data for each column.

```php
<?php

namespace Database\Factories;

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

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            "name" => $this->faker->name(),
            "email" => $this->faker->unique()->safeEmail(),
            "username" => $this->faker->unique()->userName(),
            "mobile_no" => $this->faker->phoneNumber(),
            "password" => Hash::make("Password"),
            "email_verified_at" => now(),
            "remember_token" => Str::random(10)
        ];
    }
}
```

Now to run the factory you can use Laravel Tinker.

In the terminal, you can run the command below to enable Laravel Tinker.

```php
php artisan tinker
```

Now run the factory using the model to generate a single record with fake data.

```php
App\Models\User::factory()->create();
```

To generate more than one record you have to specify the `count` for records.
```php
App\Models\User::factory()->count(10)->create();
```

## DB Seeder

To make a seeder For UserTableSeeder by running the command below.

```php
php artisan make:seed UserTableSeeder
```

This command generate a file in `database/seeders/UserTableSeeder.php`

Next Update the run function of the seeder file.

```php
<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;

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

Now Run the command below to seed the data,

```php
php artisan db:seed --class=UserTableSeeder
```

You can watch the explanation video for more clarity.

### Complete Video Tutorial on YouTube.
%[https://youtu.be/dROeStI4uvI

If you face any issues while implementing, please comment with your query.

Thank You for Reading

Reach Out To me.
[Twitter](https://twitter.com/techtoolindia)
[Instagram](https://www.instagram.com/techtoolindia/)
[YouTube](https://www.youtube.com/c/TechToolIndia)
