How to fetch data by using Laravel Eloquent Relati

2019-09-06 16:22发布

问题:

I'm new to Laravel. Currently trying to use eloquent in one of my application. I've 3 database tables.. trips, trips_album, album_images. Table column are as follows..

trips - id(auto increment), trip_title, about_place, created_by
trips_album - id(auto_increment), trips_id(pointing to 'id' of trip table), album_status
album_images - album_id(pointing to 'id' of trip_album table), img_name

So, now i want all album images of trips, which belong to any particular user. How can i do this by using Laravel Eloquent Relationships. I'm using Laravel 4.2.

回答1:

The first, you may setting somethings.

class Trip extends Eloquent {
    public $table = "trips";
}
class Album extends Eloquent {
    public $table = "trips_album";
}
class Image extends Eloquent {
    public $table = "album_images";
}

After that, using "Has Many Through". Document Here

class Trip extends Eloquent {
    ...

    public function Images()
    {
        return $this->hasManyThrough('Image', 'Album', 'trip_id', 'album_id');
    }
}