I am having problem with available
key in the map
collection.
The available
key use contains
method. It should return true if the value of product id in $unavailableProducts does not contain in $products ($value->product_id == $product->id)
What did I do wrong?
$unavailableProducts = $this->unavailableProducts();
$products = $this->products->all();
$allProducts = $products->map(function ($product) use($unavailableProducts) {
return [
'id' => $product->id,
'title' => $product->title,
'available' => $unavailableProducts['result']->contains(function ($value, $key) use ($product) {
if ($value->product_id == $product->id) {
return false;
}
return true;
}),
];
});
First, make sure that
$unavailableProductions['result']
is a collection.Second, change your
contains
method like this:The
$unavailableProducts['result']->contains('id', $product->id)
will determine if the$unaviableProductions['result']
collection has a keyid
where the value is$product->id