How to Run Specific Seeder in Laravel 8 Tutorial

Share this Article
Reading Time: 4 minutes
826 Views

In this article we will see the concept i.e How to run specific seeder in laravel 8. 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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.

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 8 Tutorial Tutorial in a very detailed way.

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.

Find More on Laravel 8 Articles here