0

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!

1 Answers1

0

Edit your controller to

class PublicationController extends Controller
{
public function index()
{
    $publications = Publication::orderBy('year', 'DESC')
        ->orderBy('month', 'DESC')
        ->get();
    return $publications->groupBy('year');
 }
}
Hussein
  • 1,143
  • 1
  • 9
  • 16
  • Thanks! But i think, in this way, i can't choose which columns return or not, right? I want to select columns i return. If you know how to do, please tell me. – Ryutaro Matsumoto Apr 12 '18 at 03:01
  • to choose the columns you want, select them from db via `Model::select('column')->get();`, this will still return collections which you can use `groupBy()` later on – Hussein Apr 12 '18 at 03:05
  • Thank you In this way, i cannot change the structure of json response, right? For example, ` '2014' => [ 'Jan' => [ 'people' => [ 'Mr.Apple', 'Ms. Orange'] ] ] ` Like this – Ryutaro Matsumoto Apr 12 '18 at 06:47
  • you can do this while querying the db, and another way to to use `collection->toArray()` then change whatever you need – Hussein Apr 12 '18 at 06:57