beginner Question :
I have query like this :
$arr1 = BillProduct::where('bill_id', '=', $bill->id)->get('quantity');
$arr2 = BillProduct::where('bill_id', '=', $bill->id)->get('product_id');
and its return like this :
[[{"quantity":15}], [[{"product_id":2}]
how can i get just digit like 15, 2 ??
and how get in foreach next to each other? like
15,2
18,3
…
return in 2 variable like :
return $arr1, $Arr2;
3
Answers
You can do
If multiple use
foreach
loop. ExEdit 01
Merge columns of two array
Starting from PHP 5, it is possible to merge the columns of two associative arrays using the
array_replace_recursive()
function.toArray()
More information: https://www.php.net/manual/en/function.array-replace-recursive.php
Assuming your product_id does not appear multiple times in the result, you can use the second argument to
pluck()
to assign another column as the key:You save one query now 🙂