Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vinubangs's avatar

Laravel Join 3 tables , one data from first two table and multiple row from third table

I have 3 tables.

1st table- journals

id | journal_name
6 | xyz journal
7 | abc journal

2nd table- volumes

id | journal_id | volume_name
1 | 6                  | volume1
2 | 6                  | volume2
3 | 7                  | volume3

3rd table- articles

id | journal_id | volume_id | articles
1 | 6                  |          1           | volume1
2 | 6                  |           1        | volume2
3 | 7                  |           3         | volume3
4 | 7                  |           3         | volume4

Now, If clicks on journalname, then volumename shows and clicks on volumename then related articles open.

But I need one time journal_name and volume_name. AND multiple times articles.

CONTROLLER METHOD IS:

public function viewarticles($id)
    {
        $articles = article::where('volume_id',$id)->with('journal','volume')->get();
        return view('viewarticles',compact('articles'));
    }

ARTICLE MODEL IS:

function journal()
     {
          return $this->belongsTo(journal::class, 'journal_id');
     }
     function volume()
     {
          return $this->belongsTo(volume::class, 'volume_id');
     }

It is giving all tables multiple times. What to do.

0 likes
2 replies
Shahrukh4's avatar

Actually you don't need to put additional journal_id in your articles table. volume_id is sufficient to take data out in whichever way you like through journal->volume->article or so.

eusapxai's avatar
public function viewarticles($id)
    {
        $articles = article::where('volume_id',$id)->with('volume', 'volume.journal')->get();
        return view('viewarticles',compact('articles'));
    }

@vinubangs use something like this

Please or to participate in this conversation.