Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Eloquent: Advanced Relationships

Go beyond basic relationships with these advanced Eloquent relationship techniques.

24 December 20251 min readLaravelاقرأ بالعربية

On this page

  1. Has One Through
  2. Many to Many with Pivot Data
  3. Polymorphic Relationships

Master advanced Eloquent relationships:

Has One Through

php
public function latestPost()
{
    return $this->hasOneThrough(Post::class, Author::class)->latest();
}

Many to Many with Pivot Data

php
$user->roles()->attach($roleId, ['expires_at' => now()->addYear()]);

Polymorphic Relationships

php
public function commentable()
{
    return $this->morphTo();
}

These relationships handle complex data structures elegantly.

All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read