This part is calling a function (most likely a query builder)
$category->products();
//Should be
$category->products;
Also, I hope that you preload your relations? If not this will give you alot of database queries.
I need to get all product related to category id in my many to many relationship but i have not succeed yet any help please.
$ProductCategories = $product->categories;
$productsOfThisCategory = array();
foreach($ProductCategories as $category) {
$productsOfThisCategory[] = $category->products();
}
dd($productsOfThisCategory);
all categories are in $ProductCategories var but when in apply foreach and get product related to it will give me relationship but no product why ?
Oh sorry forgot that it does not mutate
$productsOfThisCategory = collect([]);
foreach($ProductCategories as $category) {
$productsOfThisCategory = $productsOfThisCategory->merge($category->products);
}
Please or to participate in this conversation.