I have foreach I need return each object in this array in JSON
Let me present my controller first
$childCategory = ChildCategory::whereProductCategoryId( $catId )->get();
$data = array();
foreach ( $childCategory as $childCat ) {
$checkChildes = ChildCategoryTwo::whereChildCategoryId( $childCat->id )->count();
$hasChildes = $checkChildes > 0 ? "Yes" : "No";
$data[] = $childCat->id;
$data[] = $childCat->image;
$data[] = $childCat->bg_color;
$data[] = $childCat->product_category_id;
$data[] = $childCat->translations[0]->name;
$data[] = $childCat->translations[1]->name;
$data[] = $hasChildes;
}
return response()->json( [$data] );
the output of data comes like this
[
1,
"1561015312.png",
"#a9dabf",
1,
"Drinks",
"No",
2,
"1562500737.jpg",
null,
1,
"Drinks",
"Yes"
]
the output is right but the format is wrong, I need it to output something like this
[
{
id : 1,
image : "1561015312.png",
color : "#a9dabf",
parent_cat : 1,
name : "Drinks",
has_child : "No",
},
{
id : 2,
image : "1562500737.jpg",
color : null,
parent_cat : 1,
name : "Soda Drinks",
has_child : "Yes"
}
]
$data[]['id'] = $childCat->id;...?{ "id": 1 }, { "image": "1561015312.png" }, { "bg_color": "#a9dabf" },witch is not what I want.grouping your data in the application properly