DEV Community 👩‍💻👨‍💻

Cover image for DB SEEDER & ELOQUENT FACTORY IN LARAVEL 9
TechTool India
TechTool India

Posted on

DB SEEDER & ELOQUENT FACTORY IN LARAVEL 9

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

Eloquent Factory

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

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

php artisan make:factory UserFactory --model=User
Enter fullscreen mode Exit fullscreen mode

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

Now let's add fake data for each column.

<?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)
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

Now to run the factory you can use Laravel Tinker.

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

php artisan tinker
Enter fullscreen mode Exit fullscreen mode

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

App\Models\User::factory()->create();
Enter fullscreen mode Exit fullscreen mode

To generate more than one record you have to specify the count for records.

App\Models\User::factory()->count(10)->create();
Enter fullscreen mode Exit fullscreen mode

DB Seeder

To make a seeder For UserTableSeeder by running command below.

php artisan make:seed UserTableSeeder
Enter fullscreen mode Exit fullscreen mode

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

Next Update the run function of seeder file.

<?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();
    }
}
Enter fullscreen mode Exit fullscreen mode

Now Run the command below to seed the data,

php artisan db:seed --class=UserTableSeeder
Enter fullscreen mode Exit fullscreen mode

You can watch the explanation video for more clarity.

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

Thank You for Reading

Reach Out To me.
Twitter
Instagram
YouTube

Top comments (1)

Collapse
 
mannydiera profile image
Manny Diera

Excellent. Very clear and to the point. Thanks!

DEV

Thank you.

 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.