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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community