'Laravel Eloquent foreign key

I have tables with foreing key that have another foreing key to other table and i need to join with eloquent this 3 tables.

I have the tables :

Films
 - ID
 - FilmsName
FilmsTags
 - FilmID
 - TagID
Tags
 - ID
 - Tagname

In the Eloquent mode i have:

Films
 public filmstags_items(){
   return $this->hasMany(FilmsTags::class, 'FilmID', 'id');
 }

FilmsTags
 public film_item(){
  return $this->hasOne(Films::class, 'id', 'FilmID');
 }

 public tag_item(){
  return $this->hasOne(Tags::class, 'id', 'TagID');
 }

Tags
 public filmstags_items(){
  return $this->hasMany(FilmsTags::class, 'TagID', 'id');
 }

Can i make something like this on my eloquent model Films to join the tables?

public join_all(){
  return $this->hasMany(FilmsTags::class, 'FilmID', 'id')->hasOne(Tags::class, 'TagID', 'id');
}

If not, please tell me how to do it with eloquent. I know i can make a join sentence with SQL but i want to learn eloquent.

Thank you.



Solution 1:[1]

At film model:

public function direct_film_tags()
{
    return $this->hasManyThrough(Tag::class, FilmTag::class, 'FilmID', 'TagID');
}

You can call like that:

Film::with('direct_film_tags')->all();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Deniz Gölbaş