Laravel 9 Seed Multiple Records Tutorial

In this Tutorial, I will demonstrate how to insert multiple records in Seeder using the 5+,6,7,8,9 operators.

Create Laravel Project

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

Also Read : Rest API Authentication with Passport Laravel 9

Create Category Model with table

php artisan make:model Category -m

Categories Table

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('category_name');
            $table->timestamps();
        });
    } 

Create Category Seeder

php artisan make:seeder CategorySeeder
<?php

namespace Database\Seeders;

use App\Models\Category;
use Illuminate\Database\Seeder;

class CategorySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $lapton = Category::create(['category_name' => 'laptop']);
        $phone = Category::create(['category_name' => 'phone']);
    }
}

Also Read : Laravel 9 Multiple Database Connection Tutorial

Now Add CategorySeeder Class in DatabaseSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(CategorySeeder::class);
    }
}

Now run, 

php artisan db:seed  

Also Read : Laravel 9 Shopping Cart Tutorial with Ajax Example

1 Comment

Leave a Reply