问题
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