I'm trying to group the json response in Laravel 5.5
My table is following
+------+-------+-------------------------------------------------------
--------------------------------------------------+
| year | month | people
|
+------+-------+-------------------------------------------------------
--------------------------------------------------+
| 2017 | 3 | 松本 知実
|
| 2016 | 12 | 中島 春香,中津川 充,村山 裕樹,杉山 翼,中島 春香,木村 稔
|
| 2017 | 3 | 桐山 裕太,吉本 稔,藤本 稔
|
| 2015 | 3 | 喜嶋 学,若松 康弘,鈴木 直子,宮沢 幹
|
| 2017 | 11 | 大垣 晃,江古田 里佳,野村 康弘,中村 美加子,喜嶋 裕太
|
| 2016 | 6 | 中島 春香,中津川 充,村山 裕樹,杉山 翼,中島 春香,木村 稔
|
| 2015 | 11 | 野村 英樹,工藤 智也,山本 七夏,青田 舞
|
and i wanna get the following json response
"2015" : {
"3" : {
"people" : "喜嶋 学,若松 康弘,鈴木 直子,宮沢 幹"
},
"11" : {
"people" : "野村 英樹,工藤 智也,山本 七夏,青田 舞"
},
},
"2016" : {
"6" : {
"people" : "中島 春香,中津川 充,村山 裕樹,杉山 翼,中島 春香,木村 稔"
},
"12" : {
"people" : "中島 春香,中津川 充,村山 裕樹,杉山 翼,中島 春香,木村 稔"
},
},
"2017" : {
"3" : {
[
"people" : "松本 知実",
"people" : "桐山 裕太,吉本 稔,藤本 稔",
]
},
"11" : {
"people" : "大垣 晃,江古田 里佳,野村 康弘,中村 美加子,喜嶋 裕太"
},
}
But i can't group the people data by same year and same months... My code is here.
My Controller
class PublicationController extends Controller
{
public function index()
{
$publications = Publication::orderBy('year', 'DESC')
->orderBy('month', 'DESC')
->get();
return PublicationResource::collection($publications);
}
}
and My Resource
class PublicationResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
$this->year => [
$this->month => [
'people' => $this->people,
],
],
];
}
}
But now in my code, the response is following.
"2017": {
"11": {
"people": "大垣 晃,江古田 里佳,野村 康弘,中村 美加子,喜嶋 裕太"
}
},
"2017": {
"3": {
"people": "松本 知実"
}
},
"2017": {
"3": {
"people": "桐山 裕太,吉本 稔,藤本 稔"
}
},
"2016": {
"12": {
"people": "中島 春香,中津川 充,村山 裕樹,杉山 翼,中島 春香,木村 稔"
}
},
"2016": {
"6": {
"people": "中島 春香,中津川 充,村山 裕樹,杉山 翼,中島 春香,木村 稔"
}
},
"2015": {
"11": {
"people": "野村 英樹,工藤 智也,山本 七夏,青田 舞"
}
},
"2015": {
"3": {
"people": "喜嶋 学,若松 康弘,鈴木 直子,宮沢 幹"
}
},
I wanna group my data by same year and same months.
How should i change my code?
I'm sorry I'm a newer of laravel. so please help me!