Reputation: 12327
Im generating an array in php to see if a user gets discount on the products in his cart. My code:
public function actionPrices($request, $action)
{
$cartId = $request->cookie('cartid');
$currentCart = Cart::findOrFail($cartId);
$items = CartItem::where('cart_id','=',$currentCart->id)->get();
$foo = array();
$actionFilters = ActionFilter::where('discount_action_id', $action->id)->lists('filter_options_id');
//if there are no $actionfilters nothing needs an extra check
if (count($actionFilters) == 0)
{
foreach ($items as $item)
{
for($i = 0; $i < $item->amount; $i++)
{
array_push($foo, $item->ProductSize->product->Priceval);
}
}
}
else
{
foreach ($items as $item)
{
$product = $item->ProductSize->product;
$filters2 = ProductFilter::where('product_id', '=', $product->id)->lists('filter_option_id');
$inArray = false;
foreach ($actionFilters as $f) {
//CODE CRASHED CHERE
if (in_array($f, $filters2)) {
$inArray = true;
}
}
if ($inArray)
{
for($i = 0; $i < $item->amount; $i++)
{
array_push($foo, $item->ProductSize->product->Priceval);
}
}
}
}
sort($foo);
return $foo;
}
Hos is it possible it crashes at that spot?
The error suggests argument 2 ($filters2) is not an array. But I get $filters2 with lists() function which returns an array. from the docs
If you would like to retrieve an array containing the values of a single column, you may use the lists method.
How should I avoid the error?
Upvotes: 0
Views: 4351
Reputation: 3967
This is because the Query Bulders's lists
method returns a Collection () Object of Illuminate\Support\Collection
So you can do this
$filters2 = ProductFilter::where('product_id', '=', $product->id)->lists('filter_option_id')->toArray();
i.e. converting the collection to array. Or...
if ($filters2->contains($f)) {
$inArray = true;
}
i.e. using method of Collection instead of php array.
See more
Edit: Oops! Laravel doc incorrectly mentions: the lists returns an array.
Upvotes: 1
Reputation: 746
Maybe $filters2 won't be an array for every case. Try something quick and dirty like:
//CODE CRASHED CHERE
if (is_array($filters2) && in_array($f, $filters2)) {
$inArray = true;
}
Upvotes: 0