
Laravel provides various types of relationships to define the associations between different models.
1. One-to-One Relationship :
A one-to-one relationship is used when a single record in one model is associated with a single record in another model.
Example: User and Address
Each user has one address, and each address belongs to one user.
Define hasOne in the User Model:
public function address() {
return $this->hasOne(Address::class, 'user_id', 'id'); // 'user_id' is the foreign key in the 'addresses' table
}
Fetch Users with Their Addresses:
Route::get('/get-users', function() {
$users = User::with('address')->get();
return view('user', ['users' => $users]);
});
Define belongsTo in the Address Model:
public function user() {
return $this->belongsTo(User::class, 'user_id', 'id'); // 'user_id' links addresses to users
}
Fetch Address with Their Users:
Route::get('/get-addresses', function() {
$addresses = Address::with('user')->get();
return view('address', ['addresses' => $addresses]);
});
2. One-to-Many (hasMany()) :
A one-to-many relationship is used when one record in a model is associated with multiple records in another model. For example, a user can have multiple posts, but each post belongs to one user.
Example: User and Posts
1. In the app\models\User model:
public function posts() {
return $this->hasMany(Post::class, 'user_id', 'id'); // 'user_id' is the foreign key in the 'posts' table
}
1. In the app\models\Post model:
public function user() {
return $this->belongsTo(User::class, 'user_id', 'id'); // 'user_id' links posts to users
}
Fetch Users with Their Posts and Addresses:
Route::get('/get-users', function() {
$users = User::has('posts') // Filters users who have at least one post
->with(['address', 'posts']) // Eager loads related data
->get();
return view('user', ['users' => $users]);
});
Fetch Posts with Their Associated Users:
Route::get('/get-posts', function() {
$posts = Post::with('user') // Eager loads the user associated with each post
->limit(20) // Limits the result to 20 posts
->get();
return view('posts', ['posts' => $posts]);
});
Testing in Blade Views user.blade.php :
@foreach($users as $user)
<h2>{{ $user->name }}</h2>
<p>Address: {{ $user->address->street ?? 'No address' }}</p>
<h3>Posts:</h3>
<ul>
@foreach($user->posts as $post)
<li>{{ $post->title }} - {{ $post->created_at }}</li>
@endforeach
</ul>
@endforeach
3. Many-to-Many (belongsToMany()) :
A many-to-many relationship is used when records in one model can be associated with multiple records in another model, and vice versa. For example:
- A Post can have multiple Tags.
- A Tag can have multiple Posts.
- dd
A pivot table is used to manage this relationship. This table acts as an intermediary, containing the foreign keys of both models.
Step-by-Step Implementation
1. Defining Relationships in Models
- In the app\models\Post model:
public function tags() {
return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id');
}
- post_tag: The pivot table name.
- post_id: Foreign key for the posts table.
- tag_id: Foreign key for the tags table.
- In the Tag Model:
public function posts() {
return $this->belongsToMany(Post::class, 'post_tag', 'tag_id', 'post_id');
}
2. Fetching Data
- Get Posts with Tags:
Route::get('/posts', function() {
$posts = Post::with('tags')->get();
return view('post.index', ['posts' => $posts]);
});
- Get Tags with Posts:
Route::get('/tags', function() {
$tags = Tag::with('posts')->get();
return view('tag.index', ['tags' => $tags]);
});
3. Blade Views
- Display Posts and Their Tags: post/index.blade.php
@foreach ($posts as $post)
<h2>{{ $post->title }}</h2>
<ul>
@foreach ($post->tags as $tag)
<li>{{ $tag->name }}</li>
@endforeach
</ul>
@endforeach
- Display Tags and Their Posts: tag/index.blade.php
@foreach ($tags as $tag)
<h2>{{ $tag->name }}</h2>
<ul>
@foreach ($tag->posts as $post)
<li>{{ $post->title }}</li>
@endforeach
</ul>
@endforeach
4. Creating the Pivot Table
Run the following Artisan command to create the post_tag pivot table migration:
php artisan make:migration create_post_tag_table --create=post_tag
* Please Don't Spam Here. All the Comments are Reviewed by Admin.