Support the ongoing development of Laravel.io →
Database Eloquent Queues
Last updated 2 years ago.
0

That's some weird code in your controller? I you want to filter your shops that have products from a certain subcategory, you just do:

$data = Shop::query()->whereHas('products', function($q) use ($subcategory) {
            $q->where('sub_category',$subcategory);
})->get();

Note: to follow conventions, your relation in Shop should be "products", not "product", because a shop has many products.

What do you mean with duplicate data? The query will get every Shop model that has at least one Product model with sub_category = $subcategory.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

lycans28 lycans28 Joined 24 May 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.