dimanche 6 mars 2016

HasMany Relationships as combined collection

I have a Merchant model, this Merchant model has many Categories and each Category has many Products.

Now for my Product index I would like to list all Products for a given Merchant.

$categories = Category::where('merchant_id', 1);

foreach ($categories as $category) {
    $products[] = Product::where('category_id', $category->id);
}

This code would give me an array of collections of all the Products for the Merchant with id = 1.

How can I combine the collections so I can pass them to my view?

return view('products.index', compact('products'));



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire