How To Insert Dummy Records Using Seeder In Laravel

Sometimes we are testing your Laravel app that time we need some records in the database. So, we are added manually to fake records in the database.

The factory, faker, seeders, and tinker commands are used to generate and create the Laravel fake and dummy data in the database. Using this, we can add more than a thousand records to the database in minutes.

In this article, we are going to learn how to insert dummy records using seeders in Laravel.

Step 1: Install Laravel APP

Let’s start by creating a new project named Codhubs. To do this, run the following command:

composer create-project laravel/laravel Codehubs

Install and create a new Laravel project.

Step 2: Configure Database with Codehubs App

After installing the Codehubs Laravel application we will need to configure our database for it to work.

Go to http://localhost/phpmyadmin/

Create a new Database. For example: create a codehubs database in my localhost.

Now Configure the database in the .env file with this app:

DB_CONNECTION=mysql // add your database connection name
DB_HOST=127.0.0.1 // add your database host name
DB_PORT=3306 // add your database port
DB_DATABASE=codehubs // add your database name
DB_USERNAME=root // add your database username
DB_PASSWORD= // add your database password

With everything configured it’s time to run our app and see what it looks like.

To run the application, type the following command:

php artisan serve

For more information on how to set up laravel project go here.

Step 3: Create Seeders Class

What’s Seeder?

The seeder is one type of command class that stands for inserting multiple rows of dummy data into the database. For testing the purpose, adds testing data into the database table using the database seeder. also, testing with various data types allows developers to detect bugs and optimize performance.

To create a seeder, execute the make:seeder command:

php artisan make:seeder PostSeeder

Now go to your database/seeders directory, There is a new seeders class that will be created.

Open the PostSeeder.php file and update it with the below code:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    $faker = \Faker\Factory::create();

        DB::table("posts")->insert([
            'title' => $faker->name(),
      'description' => $faker->text(),
        ]);
    }
}

We need to call the seeder class in the database seeder file go to the database/seeders directory, open the DatabaseSeeder.php file and add the below code inside of DatabaseSeeder class.

$this->call([
   PostSeeder::class
]);

Here is the full code of  DatabaseSeeder.php file please check this one:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();
    
    $this->call([
            PostSeeder::class
        ]);
    }
}

Step 4: Run db:seed Command

Now it’s time to execute the following command on the command prompt to generate or create dummy data using db:seed command:

php artisan db:seed

Output:

Now go to http://localhost/phpmyadmin/ and check the database posts table.

If you want to create dummy records using a factory and you don’t know how, so you can visit the how to insert dummy records using a factory in laravel article.

Thank you, I hope you find something helpful. 😀

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories