How To

Laravel 8: Database Seeder with Example

In today’s tutorial, we will learn about Database Seeder in Laravel 8 with Example. I will guide you on How to create Seeder, What is the command for Database Seeder and How to run Seeder in Laravel 8.

What is Database Seeder in Laravel?

Laravel provides a simple method to seed test data into a database using seeder classes. You can perform database seeding in Laravel to add fake data into the database for testing purposes.

Database Seeder in Laravel 8 Example

Step 1: Create Seeder Command

php artisan make:seeder UserSeeder

After you run the above command, it will create one file UserSeeder.php in the seeds folder. The seed classes are stored in the database/seeders directory.

<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */    public function run()
    {
        User::create([
            'name' => 'John doe',
            'email' => 'john@gmail.com',
            'mobile' => '911234567891',
            'password' => Hash::make('john@123')
        ]);
    }
}

Step 2: Calling Additional Seeders

The call method is used to execute additional seed classes within the DatabaseSeeder class. It allows you to break up your database seeding into multiple files so that no single seeder class becomes too large. The call method accepts an array of seeder classes that should be executed.

<?php
  
use Illuminate\Database\Seeder;
   
class DatabaseSeeder extends Seeder
{
    public function run()
    {
         $this->call([
         UserSeeder::class,
         PostSeeder::class,
     ]);
    }
}

Step 3: Running Seeders

Command to run seeder

php artisan db:seed

Command for single seeder run

php artisan db:seed -–class=UserSeeder

You can also seed your database using the migrate:fresh command in combination with the –seed option. This command drops all tables, re-runs all of your migrations, and re-building your database.

php artisan migrate:fresh --seed

Conclusion:

This way, you can create a database seeder in Laravel. If you have any doubts, connect with our experienced Laravel Developers, who will help you with your queries. Share the tutorial with your friends and stay in touch with us to learn more.

Click to rate this post!
[Total: 20 Average: 4]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer 🏅 with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket 🏏.

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

6 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago