A one-to-many relationship means one record in a table can be related to multiple records in another table.

Real-World Examples

  • One User has many Posts
  • One Category has many Products
  • One Course has many Lessons
  • One Order has many Order Items

1.Database Table Structure

Example: Users & Posts

users table

id (PK)
name
email

posts table

id (PK)
user_id (FK)
title
content

👉 Foreign Key Rule
The child table (posts) must contain the foreign key (user_id).

2.Create Models & Migrations

php artisan make:model User -m
php artisan make:model Post -m

users migration

Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});

posts migration

Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->text('content');
$table->timestamps();
});

Run migration:

php artisan migrate

3.Define Relationship in Models

Parent Model (User)

class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}

Child Model (Post)

class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}

4.How Laravel Knows the Relationship

Laravel automatically assumes:

Part Value
Foreign key user_id
Local key id
Table name posts

If naming is custom, you must define them manually.

5.Fetching Data (Read Operations)

Get all posts of a user

$user = User::find(1);
$posts = $user->posts;

Loop through posts

foreach ($user->posts as $post) {
echo $post->title;
}

6.Eager Loading (IMPORTANT for Performance)

N+1 Problem

$users = User::all();

foreach ($users as $user) {
$user->posts;
}

Correct Way

$users = User::with('posts')->get();

👉 This loads users and posts in two queries only.

7.Create Related Records

Method 1: Using relationship

$user = User::find(1);

$user->posts()->create([
'title' => 'My First Post',
'content' => 'Post content here'
]);

user_id is auto-filled

Method 2: createMany()

$user->posts()->createMany([
['title' => 'Post 1', 'content' => 'Content 1'],
['title' => 'Post 2', 'content' => 'Content 2'],
]);

8.Update Related Records

$post = $user->posts()->where('id', 5)->first();

$post->update([
'title' => 'Updated Title'
]);

9.Delete Related Records

Delete a single post

$user->posts()->where('id', 5)->delete();

Delete all posts of a user

$user->posts()->delete();

10.Count Related Records

$user->posts()->count();

Optimized counting

$users = User::withCount('posts')->get();

echo $users[0]->posts_count;

11.Conditional Relationships

$user->posts()->where('status', 'published')->get();

12.Ordering Related Data

$user->posts()->latest()->get();
$user->posts()->orderBy('title')->get();

13.Custom Foreign Key Example

return $this->hasMany(Post::class, 'author_id', 'id');
Parameter Meaning
1st Related Model
2nd Foreign Key
3rd Local Key

14.One-to-Many in Controller Example

class PostController extends Controller
{
public function index($userId)
{
$user = User::with('posts')->findOrFail($userId);
return view('posts.index', compact('user'));
}
}

15.Blade View Example

<h1>{{ $user->name }}'s Posts</h1>

@foreach ($user->posts as $post)
<p>{{ $post->title }}</p>
@endforeach

Categorized in:

Laravel,