I have a code like the one below. Let the data coming from "fiyat" and "kampanyafiyat" relations are grouped according to the column named type in these tables.
$services = Servis::with('fiyat')
->with('kampanyafiyat')
->get()
->transform(function($servis) {
return tap($servis, function($servis) {
$servis->fiyat = $servis->fiyat
->groupBy('tip');
$servis->kampanyafiyat = $servis->kampanyafiyat
->groupBy('tip');
});
})
->all();
dd($ services);
I added the code and saw on the screen that the groupBy operation was successful.
When I want it as json, groupBy disappears and comes as default.
return response()->json(['status' => true, 'services'=> $services],200);
When I express as json_encode(), groupBy gets lost.
json it sounds like the following when I want it.
[
{
"id":1,
"hizmet":"tasima",
"created_at":null,
"updated_at":null,
"fiyat":[
{
"id":1,
"servis_id":1,
"tip":"saatlik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
{
"id":3,
"servis_id":1,
"tip":"gunluk",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
{
"id":5,
"servis_id":1,
"tip":"haftalik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
}
],
"kampanyafiyat":[
{
"id":1,
"servis_id":1,
"tip":"saatlik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
{
"id":3,
"servis_id":1,
"tip":"gunluk",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
{
"id":5,
"servis_id":1,
"tip":"haftalik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
}
]
}
]
Actually, what I want to happen is as follows.
[
{
"id":1,
"hizmet":"tasima",
"created_at":null,
"updated_at":null,
"fiyat":{
"saatlik":{
"id":1,
"servis_id":1,
"tip":"saatlik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
"gunluk":{
"id":3,
"servis_id":1,
"tip":"gunluk",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
"haftalik":{
"id":5,
"servis_id":1,
"tip":"haftalik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
}
},
"kampanyafiyat":{
"saatlik":{
"id":1,
"servis_id":1,
"tip":"saatlik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
"gunluk":{
"id":3,
"servis_id":1,
"tip":"gunluk",
"yillik":"8.35",
"created_at":null,
"updated_at":null
},
"haftalik":{
"id":5,
"servis_id":1,
"tip":"haftalik",
"yillik":"8.35",
"created_at":null,
"updated_at":null
}
}
}
]
My Answer:
To achieve the desired output where the data is grouped by the 'tip' column within the 'fiyat' and 'kampanyafiyat' arrays, you can modify your transformation logic as follows:
php
$services = Servis::with('fiyat')
->with('kampanyafiyat')
->get()
->transform(function($servis) {
$fiyatGrouped = $servis->fiyat->groupBy('tip')->map(function($items) {
return $items->first();
});
$kampanyafiyatGrouped = $servis->kampanyafiyat->groupBy('tip')->map(function($items) {
return $items->first();
});
$servis->fiyat = $fiyatGrouped->all();
$servis->kampanyafiyat = $kampanyafiyatGrouped->all();
return $servis;
})
->all();
return response()->json(['status' => true, 'services' => $services], 200);
In this updated code, we are grouping the 'fiyat' and 'kampanyafiyat' arrays by the 'tip' column and then mapping over the grouped items to only keep the first item in each group. This way, the data will be structured as desired when converted to JSON.
Rate this post
4 of 5 based on 8187 votesComments