Laravel Seeder is a built-in feature in the Laravel PHP framework. It allows us to populate the database tables with dummy data during the application’s testing or development phase. The Seeder classes help developers seed databases with data in a structured and organized way. It makes it easier to test the application with different scenarios. By using this, you can populate the database with sample data. These sample data can be used to demonstrate the application’s features to clients. In this post, we will gonna implement functionality to seed data using Laravel seeder.
What is Laravel Seeder
In Laravel, Seeders are PHP classes that extend the Seeder class. It provides methods to insert data into the database. You can create custom Seeder classes or use pre-defined Seeders provided by Laravel. Both can seed the database with dummy data.
Prerequisites
We are going to implement an example of a Laravel seeder to seed the dummy data in Laravel 10. The example will consist of a Larael 10 application. Hence, for working with a Laravel 10 application, you need the below requirements.
- PHP >=8.1
- Composer
- Apache/Nginx Server
- VS Code Editor (Optional)
- MySQL (version > 5)
Once, you are done, let’s move to the example of creating a Laravel seeder.
How to Create Authentication Using Breeze Auth in Laravel 10
Step 1 – Create Laravel Project
For creating the Laravel project, you need to hit the below command in the terminal.
composer create-project --prefer-dist laravel/laravel my-app
After the project setup, you need a database configuration.
Step 2 – Configure Database For Laravel Project
You need to create a database in MySQL first. After that, you have to configure the database in the project. Hence, navigate to the .env file and add the database configuration as shown below.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={{ DATABASE_NAME }}
DB_USERNAME={{ DATABASE_USERNAME }}
DB_PASSWORD={{ DATABASE_PASSWORD }}
After the database configuration, we will be moving on to the next step.
So, in the next step, we will be creating a seeder file. For creating a seeder, you can create one using the artisan command.
How to Create Auth Scaffolding Using React JS in Laravel 10
Step 3 – Create a Model, Migration, Seeder All in Laravel 10
Previously, before Laravel 10, we had to create the seeder separately. But, now, in Laravel 10, you can create all in one together using a single command.
php artisan make:model
The command will prompt you to enter the model name. In my case, I have given the model name Employee.
Again, it will prompt you to create the necessary files like migration, factory, seeder, etc.
You can choose specific ones or create all these files. It depends on you totally.
So, now, we have the files ready along with the seeder. Now, we will add the schema in the migration file.
How to Create Dummy Data Using Tinker Factory in Laravel 10
Step 4 – Add Schema in the Migration
For adding the schema in the migration file, navigate to the created migration. Now, add the below schema there.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('gender')->nullable();
$table->string('email')->nullable();
$table->string('phone_number')->nullable();
$table->string('password')->nullable();
$table->string('address')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('employees');
}
};
Also, you need to add fillable data in the respective model.
How to Create Auth Scaffolding Using Vue JS in Laravel 10
Step 5 – Add Mass Assignment in Model
Navigate to the Employee.php model and add the below snippet in it.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
use HasFactory;
protected $fillable = [
'first_name',
'last_name',
'gender',
'email',
'phone_number',
'password' .
'address'
];
}
Next, you need to migrate the schema as you added in the migration file.
Step 6 – Migrate Database
For migrating the schema, hit the below command.
php artisan migrate
After migrating the schema, you need to add the functionality to seed test data.
How to Create Bootstrap Auth Scaffolding in Laravel 10
Step 7 – Create Test Data in Seeder in Laravel 10
As per the model and migration we created, we will be adding some test data. So, that while running the seeder, the test data will be inserted into the employee table. This data can be single or multiple based on your requirement.
<?php
namespace Database\Seeders;
use App\Models\Employee;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class EmployeeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Employee::truncate();
Employee::create([
'first_name' => 'John',
'last_name' => 'Smith',
'gender' => 'Male',
'email' => 'john@yopmail.com',
'phone_number' => '+919393493949',
'password' => Hash::make('123456'),
'address' => '#222, 5th Block'
]);
}
}
In the above seeder file, I have created an array for employee data. This data will be inserted into the employee table. So, everytime when this seeder gets execute the table will be truncated first. Using this way, it won’t make duplicate records of the employee data.
Step 8 – Register Seeder into DatabaseSeeder
In order to execute any seeder in Laravel, you need to register the class into main seeder which is database seeder.
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call([
EmployeeSeeder::class
]);
}
}
You can add N number of seeders here as shown above in the form of an array.
Step 9 – Run Seeder in Laravel 10
Lastly, you need to run the seeder using the artisan command as shown below.
php artisan db:seed
This command will run all the seeders which are added inside the DatabaseSeeder class.
In case, if you have multiple seeders added in the database seeder class, then it is not recommended to execute all seeders every time. Because, when you execute the seeders, it will wipe out the old test data from the table. So, in this case, you can run a specific seeder as per the requirement.
Laravel 10 Client Side Form Validation Using Parsley JS
Run Single Specific Seeder in Laravel 10
In order to run the single seeder, you have to run the below command in the terminal.
php artisan db:seed --class=SeederClassName
You need to replace the seeder class. In our case, it is EmployeeSeeder. Hence, I will put that class name as shown below.
That’s it. Now, you can check the database table for the added records.
The employee record is added successfully by the seeder.
Leave a Reply