How to Run Specific Seeder in Laravel 9 Tutorial

Reading Time: 4 minutes
2,529 Views

In this article we will see the concept i.e How to run specific seeder in laravel 9. In laravel we have artisan commands available by the help of which we can operate with laravel application in a very easy way.

Seeders are basically files which helps to seed dummy data into database tables.

To create migrations and run seeders we have several commands available. This tutorial helps you to understand the concept of running seeders in laravel application.

In laravel application we can either run all seeders at once, run seeder along with migration, run specific migration etc

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Seeders Location in Laravel Application

When we create seeders, it will be stored inside /database/seeders folder.

DatabaseSeeder.php is the file from where we can run all seeders for application.

There are two ways by the help of which we can run a specific seeder file when we need.

  • By using DatabaseSeeder.php file
  • By using –class flag in artisan command

Using DatabaseSeeder.php File To Seed Data

Whenever we run the artisan command like db:seed, it runs all Seeders.

$ php artisan db:seed

The above command will always call DatabaseSeeder.php to seed data or it finds the linked seeder classes from there.

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();
        $this->call(ProductSeeder::class);
        //$this->call(UserSeeder::class);
    }
}

Linked Seeder Classes

  • $this->call(ProductSeeder::class);
  • $this->call(UserSeeder::class);

Whenever we want to run only one seeder class file, do comment rest all classes.

Run Migration with Seeder

The given command helps to run migrations with seeder in laravel application.

$ php artisan migrate:fresh --seed

Run Seeders Forcefully

$ php artisan db:seed --force

Using –class flag in Artisan Command

While using db:seed command also we have a available flag by which we can run only a specific seeder class file.

The given command runs only a specified seeder class –

# Run ProductSeeder File
$ php artisan db:seed --class=ProductSeeder

# Run UserSeeder File
$ php artisan db:seed --class=UserSeeder

Run Specific Seeder Forefully

$ php artisan db:seed --class=UserSeeder --force

We hope this article helped you to learn How to Run Specific Seeder in Laravel 9 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more