How to Run Specific Seeder To Insert Records in Laravel 9

Last updated on: by Digamber

This guide will teach you how to run specific seeders in the Laravel 9 application and easily add records or data to the database.

Laravel Seeders are classes that populate the database with sample data records for web testing or development purposes.

Seeding is the process of loading a database with sample data for testing, development, or demonstration objectives. It helps web developers test the application with realistic data without manually adding it every time the database is reset or recreated.

We will show you how to use a specific seeder class to populate the database in Laravel.

Seeding is typically performed using a command-line interface, and the data can be easily re-populated if the database needs to reset.

Laravel 9 Insert Sample Data in Database with Specific Seeder Example

  • Step 1: Build New Project
  • Step 2: Create Database Connection
  • Step 3: Run Migrations in Laravel
  • Step 4: Create Specific Seeder Class in Laravel
  • Step 5: Register New Seeder Class
  • Step 6: Run Seeders and Migrate

Build New Project

In order to install a new Laravel project, you have to first install the Composer. It is a dependency manager and you can get it from here.

To create a new Laravel project, you have to open the terminal or command prompt and then evoke the suggested command to create a new Laravel project:

composer create-project laravel/laravel --prefer-dist laravel-blog

Move into the project directory:

cd laravel-blog

Create Database Connection

By default Laravel is connected to MySQL. In order to make the consensus to the database, you need to specify the following details in the .env configuration file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Run Migrations in Laravel

Laravel migration is a developer-friendly feature in Laravel that lets developers handle changes in a structured and organized manner.

It provides a version control system for the database schema and makes it easy to manage changes, such as creating tables, modifying columns, or rolling back changes.

The migrations reside in the “database/migrations” folder in a Laravel project.

To run migrations, you can take help of the Artisan command line tool.

php artisan migrate

Create Specific Seeder Class in Laravel

To create a specific seeder class in Laravel, you have to totally rely on the artisan command.

Make sure to go to terminal then run the following command:

php artisan make:seeder UserSeeder

The above command will generate a brand new UserSeeder.php file that you can check by visiting the database/seeders folder.

You need to add the data in the run method that needs to be inserted into the database.

<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::create([
            "name" => "Jofra Archer",
            "email" => "archer@ecb.com",
            "password" => bcrypt("55555")
        ]);
    }
}

Register New Seeder Class

We have defined the table properties in the seeder file; now, we have to register the seeder class in the database/seeds/DatabaseSeeder.php file.

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UserSeeder::class);
        \App\Models\User::factory(20)->create();
        \App\Models\User::factory()->create([
            'name' => 'Demo',
            'email' => 'demot@example.com',
        ]);
    }
}

Run Seeders and Migrate

We are done with the database seeder setup now we will show you how to run a migration with seeder in Laravel:

php artisan db:seed

After running the above command; you will see following message on the terminal.

# INFO  Seeding database.  
Database\Seeders\UserSeeder .................................................. RUNNING  
Database\Seeders\UserSeeder ............................................ 64.44 ms DONE 

How to Run Specific Seeder To Insert Records in Laravel 9

Conclusion

Laravel is a PHP framework for building web applications. It provides an easy way to interact with databases using Eloquent, its built-in ORM (Object-Relational Mapping) tool.

In this step-by-step guide, we saw how to use seeders to insert dummy data into the database for testing and developing the laravel app.

Digamber

A Full-stack developer with a passion to solve real world problems through functional programming.