Php 何处有多重关系-laravel 5.4

Php 何处有多重关系-laravel 5.4,php,laravel-5,foreign-keys,Php,Laravel 5,Foreign Keys,我有多张桌子 旅游桌 id | title | slug ..... 图像表 id | tour_id | image..... 包含表 id | tour_id | inclusion.... id | tour_id | exclusion... 排除表 id | tour_id | inclusion.... id | tour_id | exclusion... 数列表 id | tour_id | day_summary..... 巡更用户表(这是巡更表和用户表之间的透视

我有多张桌子

旅游桌

id | title | slug .....
图像表

id | tour_id | image.....
包含表

id | tour_id | inclusion....
id | tour_id | exclusion...
排除表

id | tour_id | inclusion....
id | tour_id | exclusion...
数列表

id | tour_id | day_summary.....
巡更用户表
(这是巡更表和用户表之间的透视表)

我需要

我想通过
image、inclusion、exclude、itenary、tour用户
where
image.tour\u id
equals
tour.id
inclusion.tour\u id
equals
tour.id
等等获取旅游详情

我已将
旅游模型中的关系定义为

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);
    }
我知道这可以通过循环语句和条件语句来实现,其中我们需要多次查询数据库,但是我想通过“在Laravel 5.4中使用Elount进行急切加载”来实现。这似乎有点类似的问题,但无法解决

我试过这样的方法:

$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();
$tour=tour::where('slug',$slug)
->带有('user'、'image'、'itenary'、'inclusion'、'exclusion')
->whereHas('user',函数($query){
$query->where('user_id',user()->id);})whereHas('itenary',函数($query){
$query->where('tour_id','21');})whereHas('inclusion',function($query){
$query->where('tour_id',21');})first();

你不能在Laravel上做这样的事情,因为关系是
有很多

你应该这样做:

$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;
$tour=tour::where('slug',$slug)
->whereHas('user',函数($query){
$query->where('user\u id',user()->id);
})whereHas('itenary',函数($query){
$query->where('tour_id','21');
})whereHas('inclusion',函数($query){
$query->where('tour_id','21');
})第一个();
$images=$tour->image;
$includes=$tour->includes;
$Exclutions=$tour->Exclutions;
$highlights=$tour->highlight;
$itenaries=$tour->itenaries;

你不能在Laravel上做这样的事情,因为关系是
有很多

你应该这样做:

$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;
$tour=tour::where('slug',$slug)
->whereHas('user',函数($query){
$query->where('user\u id',user()->id);
})whereHas('itenary',函数($query){
$query->where('tour_id','21');
})whereHas('inclusion',函数($query){
$query->where('tour_id','21');
})第一个();
$images=$tour->image;
$includes=$tour->includes;
$Exclutions=$tour->Exclutions;
$highlights=$tour->highlight;
$itenaries=$tour->itenaries;