lundi 11 février 2019

Laravel Join 3 tables , one row from first two tables 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. But I need only single row from journals and volumes table. And multiple records from articles, if records are present.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire