Has Many Through relationship in Laravel means that one model can access many records of another model through a middle table or intermediate model.

One table gets related data from another table by using a third table in between.

  • One User has many Profiles
  • One Profile has many Posts

One User can access many Posts through Profiles

User -> Profile -> Post

  • Profiles table works as the middle table
  • User directly accesses posts through profiles
<?php
public function posts()
{
    return $this->hasManyThrough(
        Post::class,
        Profile::class
    );
}
?>

Fetch Data:

<?php
$user = User::find(1);

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