user2994560
user2994560

Reputation: 1309

Laravel Eloquent Multiple Join

I have a table albums, each album has multiple songs, artworks, and can belong to a number of series.

Each of the songs can have lyrics from another table.

So far I have :

routes.php

    Route::get('get_albums', function() {
        return Album::with('songs', 'artworks', 'series')->get();  
    });

Album.php model

<?php

class Album extends Eloquent {

    protected $table = 'albums';

    public function songs()
    {
        return $this->hasMany('Song');
    } 

    public function artworks()
    {
        return $this->hasMany('Artwork');
    }

    public function series()
    {
        return $this->hasMany('Serie');
    }    
}

Song.php model

<?php

class Song extends Eloquent {
    public function album()
    {
        return $this->belongsTo('Album');
    }

    public function lyric() {
        return $this->hasOne('Lyric');
    } 
}

Artwork.php model

<?php

class Artwork extends Eloquent 
{

    public function album()
    {
        return $this->belongsTo('Album');
    } 
}

Serie.php model

<?php

class Serie extends Eloquent {
    public function album()
    {
        return $this->belongsTo('Album');
    } 
}

Lyric.php model

<?php

class Lyric extends Eloquent {
    public function song()
    {
        return $this->belongsTo('Song');
    }
}

This gives me back all the albums with their songs, artworks, and series. Trying to figure out how to do the 2nd join to get the lyrics for the songs.

Upvotes: 0

Views: 484

Answers (1)

E. S.
E. S.

Reputation: 2919

You can try

Route::get('get_albums', function() {
    return Album::with('songs.lyric', 'artworks', 'series')->get();  
});

Upvotes: 1

Related Questions