Share this:

Today we are going to learn about topic seeder in laravel on How to Run Specific Seeder To Insert Records in Laravel 10. Laravel is a popular PHP framework known for its elegant syntax and robust features. One of the essential tasks in any Laravel application is populating the database with seed data. Seeders are classes used to insert predefined records into the database.

In this blog post, we will explore how to run a specific seeder to insert records in Laravel 10, the latest version of the framework. We will cover the steps involved in creating and executing a specific seeder, enabling you to populate your database with custom data effortlessly.

Table of Contents:

  1. Understanding Seeders in Laravel
  2. Creating a Specific Seeder
  3. Defining Data in the Specific Seeder
  4. Running the Specific Seeder
  5. Conclusion

Also Read : How to Seed US States in Database using Laravel 10 Seeder

Understanding Seeders in Laravel:

How to Run Specific Seeder To Insert Records in Laravel 10
How to Run Specific Seeder To Insert Records in Laravel 10

Seeders in Laravel allow developers to populate the database with predefined data. This feature is especially useful when testing or initializing an application.

By running seeders, you can quickly create test data, configure default settings, or insert initial records required for your application to function correctly.

Creating a Specific Seeder:

To create a specific seeder, you need to run the make:seeder Artisan command.

Open your terminal and navigate to your Laravel project’s root directory. Then, execute the following command:

php artisan make:seeder SpecificSeeder

This command creates a new seeder class named SpecificSeeder.php in the database/seeders directory.

Defining Data in the Specific Seeder:

Once the seeder class is created, open it and locate the run method.

This method is responsible for inserting records into the database. Within the run method, you can define the data you want to insert.

public function run()
{
    // Define your data here
}

For example, if you want to insert multiple records into a table called users, you can use the DB facade or Eloquent models to achieve this.

Here’s an example of inserting records using the DB facade:

public function run()
{
    DB::table('users')->insert([
        [
            'name' => 'John Doe',
            'email' => 'johndoe@example.com',
            'password' => bcrypt('password'),
        ],
        [
            'name' => 'Jane Smith',
            'email' => 'janesmith@example.com',
            'password' => bcrypt('password'),
        ],
    ]);
}

Running the Specific Seeder:

To run the specific seeder and insert the defined records, you need to execute the db:seed Artisan command.

Open your terminal and run the following command:

php artisan db:seed --class=SpecificSeeder

This command instructs Laravel to run the SpecificSeeder class and populate the database with the defined records.

Conclusion:

In this blog post, we explored the process of running a specific seeder to insert records in Laravel 10. We learned how to create a specific seeder using the make:seeder Artisan command and define the data within the run method.

Additionally, we covered how to execute the specific seeder using the db:seed Artisan command. By following these steps, you can easily populate your Laravel 10 application’s database with custom data, making it easier to test and initialize your project.

Remember, seeders are an integral part of Laravel’s database seeding functionality, and they provide a convenient way to populate your database with predefined data. Whether you’re setting up a fresh application or adding test data for development, using seeders saves time and effort, making your Laravel

Share this:

Categorized in: