Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bnazarov's avatar

BelongsToMany filtering query

I have 2 models. User, Styles[id|name].

Relationships are as following:

    // in user class
    public function styles() {

        return $this->belongsToMany('App\Styles', 'user_style', 'user_id', 'style_id');
    }
    // in styles class
    public function users() {
        return $this->belongsToMany('App\User');
    }

When filtering, visitors can select different styles and filter to see only users with those styles. The filter names are passed in the URL (for SEO purposes)

http://site.dev/users?city=&style=Style1,Style2

Can you please assist with the query that will return only users that have at least one of those styles.

0 likes
0 replies

Please or to participate in this conversation.