Laravel provides a method for seeding the test data for the database table. You can create n number of seeders for the tables. The seeder will contain the fields for which you will gonna insert the test data. There is are the difference between the Factory and the Seeder. Actually, the factory is used to insert the dummy records in a large amount. It uses the third-party package that is faker to insert the fake data. Whereas the seeder can be used to create small data for some fields. You can pass the real data for testing purposes. When we’ll develop an application in Laravel 8, it requires testing data. So, Laravel 8 seeder will provide the functionality to check the business logic with testing data. In this tutorial, we will create Seeder in Laravel 8 application for a table.
Before diving into the example, let’s comprehend what Laravel’s official document says on Laravel 8 seeder.
Laravel 8 Seeder
Laravel incorporates a method that is called a seeder class. This class is used for seeding the test data in the database. You can have multiple seeders for the different tables. You can find the Seeders class in the database/seeders folder. By default, there is a file with the name DatabaseSeeder.php inside this folder.
Here, the specified seeder will run. Actually, Laravel executes the seeders class from here.
How to Create Pagination in Laravel 8 with Eloquent
Prerequisites
This project is going to create with the below tools. For creating a new project set up in Laravel 8, you must require the following tools.
- PHP >= 7.3
- MySQL (version > 5)
- Apache/Nginx Server
- VS Code Editor
- Composer
You can choose the different code editor rather than VS code.
Create New Project For Laravel 8 Seeder
Before creating any seeder in Laravel 8, we will require a new project. Hence, we will create a new project in Laravel 8. I will use the composer to create the project.
composer create-project --prefer-dist laravel/laravel laravel-seeder
Here, the project setup has started. It will take a couple of minutes.
After creating the project, it required a database connection. Therefore, we will create a database and then connect to our project.
How to Use Guzzle Http in Laravel 8 For Request Handling
Establish Database Connection
Firstly, you will have to create a database. I have created in MySQL using the below command.
CREATE DATABASE laravel8_seeder;
Secondly, you will have to configure the database in the project. Consequently, go to the .env file of the project and replace the credentials.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={{DATABASE_NAME}}
DB_USERNAME={{DATABASE_USER}}
DB_PASSWORD={{DATABASE_PASSWORD}}
Manage Model and Migration
As per the MVC architecture, we know that model communicates with the database table. Hence, every model will have a corresponding table. This contains the fillable prop. It requires the name of the fields which will gonna manage dynamically. For creating a seeder in Laravel 8, it will require to have a table.
Consequently, as per this project, I will create a model and migration.
php artisan make:model Employee --migration
Here, the model and migration file has created by hitting the above command.
Open the database/migrations/timestamp_create_employees_table.php. Then change the schema as you required. I have added the below fields for the employees table.
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->string('phone')->nullable();
$table->string('dob')->nullable();
$table->string('address')->nullable();
$table->string('department')->nullable();
$table->timestamps();
});
}
Then open the app/Models/Employee.php and set the fillable data in the fillable prop.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
use HasFactory;
protected $fillable = [
'name',
'email',
'phone',
'dob',
'address',
'department'
];
}
In the next step, migrate the table using the below command.
php artisan migrate
Here all the tables are migrated successfully in the database.
Now, we are ready to create seeder in Laravel 8 application.
How to Implement Yajra DataTables in Laravel 8
Create Seeder in Laravel 8
Laravel provides the functionality to create the seeders using the make command. Here, we will create a seeder for the table. So, the seeder name must ressemble to the table.
Here, I have the Employees table. That’s why, I will create the seeder specifically for the Employee.
php artisan make:seeder EmployeeSeeder
Ultimately, the above command created a seeder inside the database/seeders folder. No wonder, if you create more seeders. All will kept inside this folder.
Now, open the EmployeeSeeder.php file and add the below code.
<?php
namespace Database\Seeders;
use App\Models\Employee;
use Illuminate\Database\Seeder;
class EmployeeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Employee::create(
[
"name" => "John Doe",
"email" => "johndoe@gmail.com",
"phone" => "9876543210",
"dob" => "1996-08-21",
"address" => "USA, California",
"department" => "HR"
]
);
}
}
I have added the key and value in the employee seeder. The key must be same as you created in the migration and fillable data.
How to Send Emails Using Twilio Sendgrid in Laravel 8
Execute the Seeder in Laravel 8
Ultimately, we came to know that how you can run the seeder? You can call seeder in two ways. If you have the single seeder then you can call it by giving its class name.
php artisan db:seed --class EmployeeSeeder
The above command has completed the seeding of EmployeeSeeder. That means it inserted the records in the employees table.
You can check the database table for the result. Here, I have the one record in this table and the record has inserted using the seeder.
Secondly, you can run the Seeder(s) from the DatabaseSeeder.php. For this, you will have to put the seeder class in the run() function.
You will have to pass the Seeder class inside the call() function. This function takes an array of the Seeder class. You can pass the multiple seeders here.
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
EmployeeSeeder::class
]);
}
}
Now, to run the seeder from the DatabaseSeeder.php file.
php artisan db:seed
The above command executed the EmployeeSeeder and created the record in the table.
So, we have executed the seeder from the Database Seeder file.
But what if you will have the multiple seeders? No wonder, you can create the more seeders as same like we created. Simply define the fields in the seeder class that you want to insert into the table.
Then call that seeder class in the database seeder file. It will execute all the seeders in a single command.
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
EmployeeSeeder::class
UserSeeder::class,
PostSeeder::class,
CommentSeeder::class,
]);
}
}
That’s the feature of Laravel. Now, you can execute all the seeder to insert the value in a single command.
How to Send Email Using Mailgun in Laravel 8
Summary
Eventually, we completed the Laravel 8 seeder tutorial. We had created the EmployeeSeeder for filling up the values in the Employees table. Also, we had seen the two different ways to run the seeder class. The first one was by giving the Seeder class name. This executed the seeder separately. Secondly, through the DatabaseSeeder.php file. You can use the second step for executing the multiple seeders. I anticipate you will comprehend all these steps to enforce the Laravel 8 seeder.
Leave a Reply