Laravel Eloquent: Advanced Relationships
December 24, 2025
•
1 min read
•
52 views
Table of Contents
Master advanced Eloquent relationships:
Has One Through
public function latestPost()
{
return $this->hasOneThrough(Post::class, Author::class)->latest();
}
Many to Many with Pivot Data
$user->roles()->attach($roleId, ['expires_at' => now()->addYear()]);
Polymorphic Relationships
public function commentable()
{
return $this->morphTo();
}
These relationships handle complex data structures elegantly.
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!