简体   繁体   中英

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.

You can try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM