I have to create an array with the categories array and its related products i'm fetching category with all related products using this query
$categoryData= Category::with('product')->get();
Data is beign fetched properly, using this loop to get data in the format given below:
foreach($categoryData as $row){
$categoriesData[] = [
'id' => $row->id,
'title' => $row->title,
];
foreach($row->product as $rowproduct){
$categoriesData['product_details'][] = [
'product_name' => $rowproduct->name,
'product_price' => $rowproduct->price
];
}
}
Format (It should be something like this) :
{
"categoriesData": {
"0": {
"id": 1,
"category_name" : "Name 1",
"product_details": [
{
"id": 1,
"product_name": Product Name,
},
],
"1": {
"id": 2,
""category_name" ": "Name 2",
"product_details": [
{
"id": 1,
"product_name": product name,
},
},
but through present loop all product is getting saved in one array. It is not saving inside category.
Any help is highly appreciated
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3nzTqjs
via IFTTT
Aucun commentaire:
Enregistrer un commentaire