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 –
- Pie Chart Integration with Laravel 8 – HighCharts Js
- Resize Images in Laravel 8 Before Uploading to Server
- REST Api Development in Laravel 8 with JWT Authentication
- REST Api Development in Laravel 8 with Passport
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
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.
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.