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

tomasosho's avatar

Search for products or username with vendor role

How do i search for products or users with username, slug or email that have the vendor role?

$product = Product::where ( 'p_name', 'LIKE', '%' . $q . '%' )->with('user')->get()
                OR User::whereHas(
                        'roles', function($q){
                            $q->where('name', 'Vendor');
                        }
                    )->get();
0 likes
3 replies
tykus's avatar

You want a collection comprising Products and Users????

tomasosho's avatar

yes, products or users that have the role Vendor for a search query

tomasosho's avatar

something like this

$product = Product::where ( 'p_name', 'LIKE', '%' . $q . '%' )->with('user')->get()
                OR User::whereHas(
                        'roles', function($q){
                            $q->where('name', 'Vendor');
                        } ->Where('username','LIKE', '%' . $q . '%')->orWhere('slug','LIKE', '%' . $q . '%')->orWhere('email','LIKE', '%' . $q . '%');
                    )->get();

Please or to participate in this conversation.