问题描述
我有多个表。游览表
id |标题| slu ..... .....
图片表
id | tour_id |图像.....
包含表
id | tour_id |包含....
排除表
id | tour_id |排除...
Itenary Table
id | tour_id | day_summary .....
Tour_User表(这是一个数据透视表旅游表和用户表)
tour_id | user_id
我需要
我想用图片,包含,排除,itenary,tour用户其中 image.tour_id 等于 tour.id 和 inclusion.tour_id 等于 tour.id 和 exclusion.tour_id 等于 tour.id 等。
我将 Tour Model 中的关系定义为
public function user(){
return $ this-> hasOne(TourUser :: class);
public function image(){
return $ this-> hasMany(TourImage :: class);
public function inclusion(){
return $ this-> hasMany(TourInclusion :: class);
public function exclusion(){
return $ this-> hasMany(TourExclusion :: class);
public function highlight(){
return $ this-> hasMany(TourHighlight :: class);
$ b $ public function itenary(){
return $ this-> hasMany(TourItenary :: class);
$ b我知道这可以用循环和条件语句来完成,我们需要查询数据库多次,但是我想用Laravel 5.4中的雄辩加载来实现它。我该如何实现它。 这似乎有点类似的问题,但不能解决方案。
我试过类似的东西:
$ tour = Tour ('slug',$ slug)
- > with('user','image','itenary','inclusion','exclusion')
- > whereHas(' ('user_id',user() - > id);})< - user()在登录用户的帮助函数中定义详细信息
- > whereHas('itenary',function($ query){
$ query-> where('tour_id','21');})< - pass tour id
- > whereHas('inclusion',function($ query){
$ query-> where('tour_id','21');})< - pass tour id
- > first();
解决方案在Laravel上你不能这样做,因为关系是有很多
您应该这样做:
($'$'),$($'$' $(user_id),user() - > id);
})< - user()在助手函数中定义用于登录的用户详细信息
- > whereHas ('itenary',函数($ query){
$ query->其中('tour_id','21');
})< - 通过游览ID
- > ; whereHas('inclusion',function($ query){
$ query-> where('tour_id','21');
})< - 传入游览ID
- >首先();
$ images = $ tour->图片;
$ inclusions = $ tour->包含;
$ exclusions = $ tour->排除;
$ highlights = $ tour-> highlight;
$ itenaries = $ tour-> itenary;
I have multiple tables.
Tour Table
id | title | slug .....
Image Table
id | tour_id | image.....
Inclusion Table
id | tour_id | inclusion....
Exclusion Table
id | tour_id | exclusion...
Itenary Table
id | tour_id | day_summary.....
Tour_User Table (This is a pivot table between tour table and user table)
tour_id | user_id
I Need
I want to get tour details with image, inclusion, exclusion, itenary, tour user where image.tour_id equals tour.id and inclusion.tour_id equals tour.id and exclusion.tour_id equals tour.id and so on.
I have defined relationship in Tour Model as
public function user() { return $this->hasOne(TourUser::class); } public function image() { return $this->hasMany(TourImage::class); } public function inclusion() { return $this->hasMany(TourInclusion::class); } public function exclusion() { return $this->hasMany(TourExclusion::class); } public function highlight() { return $this->hasMany(TourHighlight::class); } public function itenary() { return $this->hasMany(TourItenary::class); }
I know this can be done with looping and conditional statements where we need to query the database multiple times, but I want to achieve it with `eager loading with eloquent in Laravel 5.4 How can I achieve it. https://stackoverflow.com/a/21380265/4807414 this seem to have have a bit similar problem but couldn't solve.
I tried something like:
$tour = Tour::where('slug', $slug) ->with('user', 'image', 'itenary', 'inclusion', 'exclusion') ->whereHas('user', function($query) { $query->where('user_id', user()->id); }) <-- user() defind in helper function for logged in user detail ->whereHas('itenary', function($query) { $query->where('tour_id', '21'); }) <-- pass tour id ->whereHas('inclusion', function($query) { $query->where('tour_id', '21'); }) <-- pass tour id ->first();
You can not do something like that on Laravel because the relationship is Has Many
You should do like this :
$tour = Tour::where('slug', $slug) ->whereHas('user', function($query) { $query->where('user_id', user()->id); }) <-- user() defind in helper function for logged in user detail ->whereHas('itenary', function($query) { $query->where('tour_id', '21'); }) <-- pass tour id ->whereHas('inclusion', function($query) { $query->where('tour_id', '21'); }) <-- pass tour id ->first(); $images = $tour->image; $inclusions = $tour->inclusion; $exclusions = $tour->exclusion; $highlights = $tour->highlight; $itenaries = $tour->itenary;
这篇关于哪里有多重关系 - laravel 5.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!