The output of a bunch of categories with products — how to make withdrawal limit?

Good evening!
Faced with this problem when I deduce the bunch:
// Receive the goods to block "Popular products"
$products = Category::with('goods_product')->where('siteID', $siteID)->where('type', 1)->where('published', 1)->get();

in the model by prescribing limit:
public function goods_product()
{
 return $this->hasMany('App\Model\Goods', 'category', 'id')->where('published', 1)->limit(8)->with('images');
}

In the vyuha, he told me reports for each category. on 8 products

How to make a restriction to the withdrawal of 8 products in total for all categories and not for each?
March 19th 20 at 09:13
1 answer
March 19th 20 at 09:15
Solution
Such a restriction is certainly impossible, at least without distortions and regular means Larabel. I think you should approach the problem from the other side. You also need the goods, so get the goods with categories and not categories of goods. Not Category::with('products') and Product::with('category')->limit(8)
Even if you manage what you have described - in the view how it will look? 1 category with 8 products and then 10 empty categories?
I therefore deduce the goods for which the category is NOT hidden

I agree with you that it would be correct to get the products with categories, but again determine that the category is hidden? - Shaun.Marquardt commented on March 19th 20 at 09:18
@Shaun.Marquardt, As communications allow. Something like Product::whereHas('category' => /* And then the filtered category in stealth and how you want */) - Dax97 commented on March 19th 20 at 09:21
@Dax97, Yes, it solved the problem, thanks, sent in the right direction)) - Shaun.Marquardt commented on March 19th 20 at 09:24

Find more questions by tags Laravel