Laravel 8 Database Seeder Example

In this tutorial you will learn about the Laravel 8 Database Seeder Example and its application with practical example.

Laravel 8 Database Seeder Example

In this tutorial, we will learn how to create database seeder in laravel 8 and what is command to create seeder and how to run that seeder in laravel 8. In this tutorial we will insert data using Database Seeder in Laravel. I’ll show you how to create database seeder class for a table, seed data in seeder, call database seeder and run database seeder. In this example, we assume that we already have admins table created and wants to seed some test data into it.

Laravel 8 Database Seeder

There are many instances where we need a populated database with test data in order to test the working of various operation. Populating Test data manually is time consuming, but Laravel provides a simple method of seeding test data in your database automatically instead doing it manually. In laravel you are required to created seeder classed per table, all these classes are stored in the database/seeds directory.

Create Seeder Using Artisan Command

Seeder class file can be generated using following artisan command:

Syntax:-

Example:-

Once the above command is executed, this will generate a seeder class file in the database/seeds directory. This class has a default run() method.

Add Data In Seeder Class file

Lets open seeder class file and within default run() method, we can populate single or multiple records as following:

database/seeds/AdminSeeder.php

Laravel Run Single Seeder

Use the bellow command to run specific seeder class:

Laravel Run Single Seeder

In this method you have to declare all your seeder in DatabaseSeeder class file. Now you have to run single command to run all listed seeder class.

database/seeds/DatabaseSeeder.php

Now, use the following artisan command to run all your seeders.

In this tutorial we have learn about the Laravel 8 Database Seeder Example and its application with practical example. I hope you will like this tutorial.