How to fetch data by using Laravel Eloquent Relatinoships

我是研究僧i 提交于 2019-12-13 04:15:50

问题


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');
    }
}


来源:https://stackoverflow.com/questions/26325533/how-to-fetch-data-by-using-laravel-eloquent-relatinoships

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!