In general, this lesson shed light on the foundational and most frequently questioned topic in the laravel domain, namely database seeders in laravel. It's no surprise that we'll publish a full memoir in the style of Laravel Database Seeder.
For several days, we have been receiving requests from our audience for laravel seeder example tutorial. Now, with your approval, let's look at an example of a laravel seeder generator.

Why is it necessary to cover the propel database seeder topic in the Laravel tutorial? In theory, when developing web apps in Laravel, we should test multiple data sets. Adding real data during the development process is a bit tough. This is the goal of Database Seeder.

It generates dummy data for you and inserts it into the database table.

It allows you to construct a single seeder or numerous seeders at the same time. You can use the data to test your Laravel app, which appears to be real to some extent.

This session provides amazing opportunity and spontaneously solves the following questions:

  • What exactly is a database seeder in Laravel?
  • How to Make a Laravel Seeder
  • Why should we utilise Laravel's database seeder?

Not to fear, I will do my best to answer all of the following questions in this guide.

Table of Contents

  • Create Seeder
  • Define Multiple Seeders
  • Run Seeders and Migrate
  • Summary

Create Seeder

To construct the Seeder especially for the Employee table, perform the following command.

php artisan make:seeder EmployeeSeeder

Finally, the command above created the seeder in the database/seeds/EmployeeSeeder.php file. No surprise, if you produce more seeds, they will be stored here as well.

public function run()
{
    Employee::create([
        'name' => 'John Doe',
        'email' => 'doejohn@gmail.com',
        'phone' => '1234567890',
        'department' => 'hr',
        'dob' => '2010-01-03',
    ]);
}

As a result, we have specified the following values to be stored in the database.

  • name
  • email
  • phone
  • department
  • dob

If you need to generate extra database seeder files, you can do so. Use the above command to generate an infinite number of database seeders.

Multiple Seeders

This tutorial has familiarised us with the single seeder as if we were developing and calling a single seeder. What if you need to generate several seeders? Do you have any ideas?

Not to worry, though. To generate a new seeder, re-run the seeder command, which is as follows.

php artisan make:seeder MyNewSeeder

Then, inside the DatabaseSeeder class, define the MyNewSeeder, and every seeder class should be conjugated here solely for the calling purpose.

Keep in mind that this is a standard but necessary procedure that must be undertaken every time you construct a new seeder or call up multiple database seeders.

/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
    $this->call([
        EmployeeSeeder::class,
        MyNewSeeder::class,
        OtherNewSeeder::class,
    ]);
}

Finally, after creating a new seeder, you may need to regenerate Composer's autoloader:

composer dump-autoload

To truly understand the larval workings, I'd like to draw your attention to the fact that you can specifically utilise the -class option to seed the required class. However, the db:seed artisan command by default seeds the DatabaseSeeder class.

php artisan db:seed
php artisan db:seed --class=YourSeederClass

The following command can also be used to seed the database.
This command is in charge of dropping all tables and restarting the migrations. Only use it when you need to rebuild your database.

php artisan migrate:fresh --seed

Run Seeders and Migrate

We've almost reached the end of this instruction; the entire narrative will most likely end here.

In the DatabaseSeeder class, open database/seeds/DatabaseSeeder.php and define your database seeder class.

<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(EmployeeSeeder::class);
    }
}

Finally, when you're finished, use the command shown below to run all of the defined seeders.

php artisan db:seed

Summary

Finally, the Laravel Database Seeder tutorial with example is finished.

I hope you have absorbed all of the steps I have shared with you regarding how database seeder works in laravel with an example.

If any of the instructions were unclear to you, please provide feedback. It will help me develop, so thank you in advance craftsmen.


Tags

Recommended Posts

View All

Laravel 9 CKeditor Image Upload With Example


install ckeditor laravel 9, ckeditor image upload not working laravel 9, how to upload image in ckeditor in laravel 9, how to upload image using ckedi...

How to Install and Use MomentJS in Laravel 9 Application


Learn how to easily install and use MomentJS in your Laravel 9 application with our step-by-step guide. Improve your date and time management today!

Laravel 9 Install React Auth Tutorial with Example


In this article, I'll demonstrate how to use the Laravel UI and React Auth scaffolding to create login, register, logout, forget password, profile, an...

Effortlessly Handle File Uploads in Laravel with FilePond


We'll walk through the process of setting up FilePond, creating a file upload form, and handling file uploads in Laravel

Laravel 9 Create Multi Language Website Example Tutorial


Learn how to create a multi-language website with Laravel 9 through our example tutorial. Develop your skills and broaden your audience reach today!