In this tutorial you will learn about the Insert data using Database Seeder in Laravel and its application with practical example.
Insert data using Database Seeder in Laravel
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.
Today in this article, we will learn how to 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.
Create Seeder Using Artisan Command
Seeder class file can be generated using following artisan command:
Syntax:-
1 |
php artisan make:seeder <TableNameSeeder> |
Example:-
1 |
php artisan make:seeder AdminsTableSeeder |
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.
database/seeds/AdminsTableSeeder.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php use Illuminate\Database\Seeder; class PostsTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // } } |
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:
Add Single Record:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class AdminsTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { DB::table('admins')->insert([ 'name' => "Keith", 'email' => 'keith@gmail.com', 'password' => bcrypt('secret'), ]); } } |
Add Multiple Records:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function run() { DB::table('admins')->insert(array( array( 'name' => "Steve", 'email' => 'steve@gmail.com', 'password' => bcrypt('secret'), ), array( 'name' => "Laura", 'email' => 'laura@gmail.com', 'password' => bcrypt('secret'), ) )); } |
Call Seeder
Laravel comes with a default seeder class named DatabaseSeeder which is executed by default when the db:seed command invoked. This class can be used to call user-defined seeders like this:
1 2 3 4 5 6 |
public function run() { $this->call([ AdminsTableSeeder::class ]); } |
Run Seeder
Once the seeder classes are setup, we are ready to run laravel seeders using following artisan command:
1 |
php artisan db:seed |
We can also run a specific seeder class directly without adding it to the DatabaseSeeder class as following:
1 |
php artisan db:seed --class=AdminsTableSeeder |
Call Seeder from Route
1 |
\Artisan::call('db:seed'); |
Call Seeder from Controller
1 |
\Artisan::call('db:seed', array('--class' => 'AdminsTableSeeder')); |
Call Seeder from Migration
1 |
\Artisan::call('db:seed'); |