Every framework gives us a way to generate and insert some fake data. Laravel also gives us a way to insert dummy data. This feature is called database seeding. There are two ways to generate fake data in laravel application. One is factory and the other is seeder class. I will show you how to create a seeder class and insert dummy data using it.

I will create a PostSeeder class to create this tutorial. Then I will insert fake posts using it. So this tutorial is going to give you a clear explation of how to insert data using seeder in laravel. I will use laravel 9 seeder example to complete this tutorial.

Let's see the example code of dbseed laravel:

laravel-9-seeder-example

Assume we have a Post model like:

app/Models/Post.php

<?php

namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Item extends Model
{
    use HasFactory;
  
    protected $fillable = [
        'title', 'user_id'
    ];
}

 

Step 1: Create Seeder

Laravel gives commands to create a seeder in laravel. so you can run the following command to make a seeder in laravel application.

php artisan make:seeder PostSeeder

 

Now update it like this:

database/seeders/PostSeeder.php

<?php

namespace Database\Seeders;

use App\Models\Post;
use Illuminate\Database\Seeder;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;

class PostSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Post::create([
            'title' => fake()->unique()->paragraph(),
            'user_id' => 1
        ]);
    }
}

 

Step 2: Connect Database

After successfully installing the laravel app and then configuring the database setup. We will open the ".env" file and change the database name, username and password in the env file.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password

 

Now run the below command to run this seeder class like this:

php artisan db:seed --class=PostSeeder

 

You can also run all your seeder class by one command like this:

php artisan db:seed

 

Now register it inside the seeder class like:

Database\Seeders\DatabaseSeeder.php

<?php
  
namespace Database\Seeders;
  
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
  
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(PostSeeder::class);
        //if you have another seeder class, just register here like above;
    }
}

 

Read also: Laravel 9 Insert Dummy Data Using Factory Seeder Example

 

Conclusion

I have tried to discuss the clear concept of laravel seeder example. Now we know how to use laravel 9 seeder multiple records. Hope this how to insert data using seeder in laravel tutorial will help you.