How is that result two arrays?
Nov 14, 2022
16
Level 15
Trying to merge two collections into one single collection with ONE array
Hi!
The title says it all. My problem is that the end result is a merged collection with TWO arrays. This does not work for me as the merged array is being returned to an Ajax function which I assume needs to see a only a single array.
So - how do I get this into one collection with one single key? Many Thanks !
Code So Far
public function product_prices($term)
{
$product = DB::table('products')
->join('categories','categories.category_id','=','products.category_id')
->select('product_id', 'price', 'products.product_name', 'products.category_id','categories.parent_id')
->where([
['product_name', 'LIKE', "$term" . '%'],
['products.status', '=', 'active'],
])
->get();
$parent = DB::table('categories')
->select('parent_id','name')
->where('category_id','=', $product[0]->parent_id )
->get();
$productParent = $product->merge($parent);
$result = $productParent->flatten()->unique();
$merged = $result->all();
echo json_encode($merged);
}
Delivers this Result
0 = {stdClass}
product_id = "abc6798"
price = "100.00"
product_name = "Install Rubber"
category_id = {int} 60
parent_id = "58"
1 = {stdClass}
parent_id = "57"
name = "Installation"
Level 104
@vincej so echo json_encode($merged); produces this:
0 = {stdClass}
product_id = "abc6798"
price = "100.00"
product_name = "Install Rubber"
category_id = {int} 60
parent_id = "58"
1 = {stdClass}
parent_id = "57"
name = "Installation"
and not this (which is JSON):
[
{
"product_id": "abc6798",
"price": "100.00",
"product_name": "Install Rubber",
// etc...
},
{
"parent_id": 57,
// etc
}
]
Please or to participate in this conversation.