In this Laravel 12 tutorial, you’ll learn how to create and seed in laravel 12 database seeder one to many relationship example using model factories and seeders. This is super helpful when you’re building blog posts, categories, products, comments, or any module where one record is linked with multiple child records.
In this comprehensive guide, you’ll learn how to seed Laravel 12 database tables with one-to-many (hasMany/belongsTo) relationships using modern approaches with factories and seeders. By the end, you’ll be able to create realistic, interconnected test data effortlessly.
Table of Contents
What is a One-to-Many Relationship?
A one-to-many relationship occurs when one model is associated with many other models. The classic example is:
- One User has Many Posts
- One Category has Many Products
- One Author has Many Articles
In database terms:
- The parent table (categories) has an ID
- The child table (posts) has a foreign key that references the parent (category_id) This relationship is expressed using Laravel’s hasMany() method on the parent model and belongsTo() on the child model.
This relationship is expressed using Laravel’s hasMany() method on the parent model and belongsTo() on the child model.
Step 1: Install Laravel Project
Open your terminal and run the following command to create a fresh Laravel project. If you already have Laravel installed, you can skip this step.
composer create-project laravel/laravel laravel-seeder-example
This command will download and install the latest version of Laravel into a folder named laravel-seeder-example.
Step 2: Create Models and Migrations
In this step, we’ll create our models and migrations. For this example, let’s assume you have Category (parent) and Post (child) models where one category has many posts.
Run the following command to generate the Category model along with its migration:
php artisan make:model Category -m
Open database/migrations/xxxx_xx_xx_create_categories_table.php and Update the migration file :
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('status')->default(true);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('categories');
}
};
Create the Post Model with Migration
Now, Run the following command for post model and migration:
php artisan make:model Post -m
Open database/migrations/xxxx_xx_xx_create_posts_table.php and Update the migration file :
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories');
$table->string('title');
$table->text('content')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Run migration:
php artisan migrate
Read Also : Laravel 12 Database Seeder Example
Step 3: Define One to Many Relationship in Models
Category Model
Open app/Models/Category.php and update:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Category extends Model
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'status',
];
public function posts()
{
return $this->hasMany(Post::class);
}
}
Post Model
Open app/Models/Post.php and update :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Post extends Model
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'category_id',
'title',
'content',
];
public function category()
{
return $this->belongsTo(Category::class);
}
}
Read Also : Laravel 12 Create, Skip, Run and Rollback Migrations
Step 4: Create Factories for Dummy Data
Factories are essential for generating realistic test data.
Category Factory
Run the command given below to create category factory:
php artisan make:factory CategoryFactory
Open database/factories/CategoryFactory.php and update:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
*/
class CategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'status' => fake()->boolean(), // random true or false
];
}
}
Post Factory
Run the command given below to create post factory :
php artisan make:factory CategoryFactory
Open database/factories/PostFactory.php and update:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Category;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class PostFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => fake()->sentence(),
'content' => fake()->paragraph(),
'category_id' => Category::factory()
];
}
}
Step 5: Create Seeder for One to Many Relationship
This is the modern, clean approach recommended for Laravel 12. Run the command given below to create seeder:
php artisan make:seeder CategoryPostSeeder
Update database/Seeders/CategoryPostSeeder.php with one to many logic:
<?php
namespace Database\Seeders;
use App\Models\Category;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class CategoryPostSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Category::factory()
->count(10)
->hasPosts(2) // Create 20 posts per category
->create();
}
}
Step 6: Register Seeder in Database Seeder
Now , Let’s Open database/seeders/DatabaseSeeder.php and update it:
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call([
CategoryPostSeeder::class,
]);
}
}
Step 7: Run Seeder
Finally, seed the database:
php artisan db:seed
Your database will now contain:
- 10 categories
- Each category has 2 posts
- Total 20 posts with correct relationships
Conclusion
In this Laravel 12 tutorial, you learned how to create and seed a one to many relationship using model factories and database seeders. With just a few structured steps, you can generate fully linked dummy data that reflects real-world scenarios like categories with posts, authors with articles, or products with reviews. This approach helps speed up development, testing, and UI building by giving you realistic data to work with right from the start. Using Laravel’s modern factory methods and clean seeder structure ensures your database seeding process stays simple, flexible, and easy to maintain as your project grows.