简体   繁体   English

Laravel:选择具有范围的枢轴列

[英]Laravel: select pivot column with scope

I Wanna Show Details, It's Simple! 我想显示详细信息,这很简单! I use ScopeFilter function in my model (Estate): 我在模型(Estate)中使用ScopeFilter函数:

public function scopeFilter($query, $zone = null,  $type = null) {
    $query->whereHas('types',function ($query) use ($type) {
        $query->whereTitle($type);
    })->whereHas('zone',function ($query) use ($zone) {
            $query->whereSlug($zone);
    });
    return $query;
}`enter code here`

were is my controller (estate controller): 是我的控制器(房地产控制器):

$estates = Estate::filter($zone,$type)->latest()->paginate(12);

and here is my relation : 这是我的关系:

public function types()
{
    return $this->belongsToMany(Type::class)->withPivot('slug');
}

I have an additional column in my many to many table like: 我的多对多表中还有一个附加列,例如:

estate_id| type_id |   slug  |
------------------------------
    a    |    b    |   a_b   |
    c    |    d    |   c_d   |

I do everything fine just need show details with where on 'slug' column in my many to many table. 我所做的一切都很好, 只需要显示详细信息以及多对多表中“子弹”列上的位置。

whereHas('types',function ($query) use ($type) { $query->where('estate_type.slug',$type); }) 

找到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM