Laravel 10 Database Seeder Tutorial Example - TechvBlogs

Laravel 10 Database Seeder Tutorial Example

In this article, You will learn How to create and use database seeder in laravel 10.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

11 months ago

TechvBlogs - Google News

The "Laravel 10 Database Seeder Tutorial Example" article is a step-by-step guide that teaches how to use Laravel 10's Database Seeder feature to populate a database with dummy data. The tutorial covers everything from setting up the database and creating seeders to running the seeding process and verifying the results. It is a useful resource for developers who want to automate the process of filling their Laravel 10 application's database with test data for testing or demonstration purposes.

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.

Laravel 10 Database Seeder Tutorial Example

Install Laravel Project

Eventually, to show you the demo of the database seeder in laravel, we need to install a fresh laravel project from the beginning. If you want, you can skip this step, but only if you have already installed the application.

composer create-project --prefer-dist laravel/laravel laravel-10-seeder

Establish Database Connection

Setup database with your downloaded/installed laravel app. So, you need to find the .env file and setup database details as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password

Create Seeder in Laravel

Use the following command to create a seeder:

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' => '[email protected]',
            'mobile' => '1234567890',
            'password' => Hash::make('john')
        ]);
    }
}

Keep in mind this is the usual but foundational step, and it needs to be followed every time you create a new seeder or for calling up multiple database seeders.

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

In Laravel, you can use the --class option with the db:seed command to specifically seed a particular seeder class instead of the default DatabaseSeeder class. This is useful when you only want to seed a particular set of data.

For example, if you have a seeder class named YourSeederClass, you can run the following command to seed only that class:

php artisan db:seed --class=YourSeederClass

Additionally, you can use the migrate:fresh --seed command to drop all tables and re-run all migrations before seeding the database. This command is useful when you want to completely rebuild your database with fresh data. However, it should be used with caution as it will delete all existing data in the database.

if you want to run all seeder at the same time then run the following command:

php artisan db:seed

Conclusion

In conclusion, Laravel's database seeding feature is a powerful tool that allows developers to populate their application's database with dummy data for testing or demonstration purposes. With Laravel's built-in Seeder classes and Model Factories, it is easy to generate test data and populate your database with it. Additionally, there are third-party packages available that offer more advanced data generation capabilities. Whether you are using Artisan commands, Model Factories, or third-party packages, seeding your database is an important step in the development process and can greatly improve the efficiency of your testing and demonstration efforts.

Comments (0)

Comment


Note: All Input Fields are required.