How to Use Factory in Database Seeder In Laravel

  Dec 2023
  ITSolutionsGuides
  Category: Laravel
How to Use Factory in Database Seeder In Laravel

Hi Developers,

Lets see How to Use Factory in Database Seeder In Laravel.Both the Seeder and the Factory are both are used to generate the dummy records. But the seeders are prefered while creating the login infromations or a single table records . Factory class can generate more records at any time depends on the amount of the required data. Since the factory faker() gives more realistic data Factory are prefored over Seeders. In this method we decalring the factory class and then we are declaring the factory in the seeder and by running the seeder we will call the factory class command and then this will generate the dummy users Since we have used the Item::factory()->count(5)->create(); factory command inside the seeder class . Once when the seeder class gets executed the facrtory class will also gets executed. So lets work How to Use Factory in Database Seeder In Laravel

Learn Laravel's Database Seeder! Use factories to seed your database with realistic data. Improve efficiency and streamline development effortlessly.

Lets Create Modal

Lets create the modal using the following artisan command,
php artisan make:model Item

app/Models/Item.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Item extends Model
{
    use HasFactory;
  
    protected $fillable = [
        'title', 'body'
    ];
}

Lets Create Seeder

Lets create the seeder using the following artisan command ,
php artisan make:seeder ItemSeeder and in the seeder file we should call the factory() to run the factory to create the dummy records.
Create Seeder In Laravel

database/seeders/ItemSeeder.php

<?php
  
namespace Database\Seeders;
  
use Illuminate\Database\Seeder;
use App\Models\Item;
  
class ItemSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Item::factory()->count(5)->create();
    }
}

Lets Create Factory Class

Lets create the factory by using the following artisan command,
php artisan make:factory ItemFactory in the factory file we should declare the type of data should be added in each of the column using the $this->faker by default faker gives more properties like name,address,text,randomDigit,word,phoneNumber,email etc.

database/factories/ItemSeeder.php

<?php
  
namespace Database\Factories;
  
use App\Models\Item;
use Illuminate\Database\Eloquent\Factories\Factory;
  
class ItemFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Item::class;
  
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'title' => $this->faker->title,
            'body' => $this->faker->text,
        ];
    }
}

Lets Run Seeder

Lets Run the Seeder using the Artisan command

Create Seeder In Laravel

php artisan db:seed --class=ItemSeeder

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides