简体   繁体   中英

Laravel: select pivot column with scope

I Wanna Show Details, It's Simple! I use ScopeFilter function in my model (Estate):

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

找到它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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