Seeders are required in Laravel 10 to populate the database with initial or test data. While executing all seeders to populate the whole database is a usual strategy, there may be times when you want to execute a certain seeder separately, targeting only specific tables or data.
This article will walk you through the process of running a specific seeder in Laravel 10. We’ll look at the commands and parameters in Laravel’s Artisan CLI for running a single seeder and seeding specific data into your database.
Read More: How to Store Data in Cache in Laravel 10 Tutorial
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
Methods To Run Seeder File
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.
Read More: Laravel 10 Upload and Save XML File Data in Database
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 execute the artisan command like db:seed, it runs all application’s 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\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use Database\Seeders\ProductSeeder; class DatabaseSeeder extends Seeder { /** * Seed the application's database. */ public function run(): void { // \App\Models\User::factory(10)->create(); $this->call(ProductSeeder::class); // \App\Models\User::factory()->create([ // 'name' => 'Test User', // 'email' => 'test@example.com', // ]); } }
Linked Seeder Class
- $this->call(ProductSeeder::class);
Whenever you want to run only one seeder class file, specify the linking of that class here.
Read More: Laravel 10 Remove Composer Package Tutorial
Run Migration with Seeder
The given command helps to run migrations with seeder (test data) in laravel application. It calls DatabaseSeeder.php file.
$ php artisan migrate:fresh --seed
Run Seeders Forcefully
It calls DatabaseSeeder.php file while data seeding.
$ 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 10 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.