Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnef_sh's avatar

Laravel 5.4: return response json groupBy created_at with static key for each group

I have this controller which group the Collection by date

public function index() {
        $poLists = ( new PurchaseOrder() )
            ->where( 'needed_quantity', '>', 0 )
            ->where( 'supplier_id', $this->guard()->user()->id )
            ->get()->groupBy( function ( $item ) {
                return [ $item->created_at->format( 'Y-m-d HH:mm:ss' ) ];
            } );
dd($poLists);
        return response()->json( $poLists, 200 );
    }

working fine and this is the output

{
    "2019-02-18 1212:0202:3939": [
        {
            "id": 2,
            "created_at": "2019-02-18 12:16:39",
            "updated_at": "2019-02-18 12:16:39"
        }
    ],
    "2019-02-18 1515:0202:0202": [
        {
            "id": 4,
            "created_at": "2019-02-18 15:21:02",
            "updated_at": "2019-02-18 15:21:02"
        },
        {
            "id": 5,
            "created_at": "2019-02-18 15:21:02",
            "updated_at": "2019-02-18 15:21:02"
        }
    ],

is there is a way to add static key for each group like this

{
    date: "2019-02-18 1212:0202:3939" [
        {
            "id": 2,
            "created_at": "2019-02-18 12:16:39",
            "updated_at": "2019-02-18 12:16:39"
        }
    ],
    date: "2019-02-18 1515:0202:0202": [
        {
            "id": 3,
            "barcode": 33254,
            "status": 0,
            "created_at": "2019-02-18 15:21:02",
            "updated_at": "2019-02-18 15:21:02"
        },
        {
            "id": 5,
            "created_at": "2019-02-18 15:21:02",
            "updated_at": "2019-02-18 15:21:02"
        }
    ],

Here is date is the static key

0 likes
5 replies
Shahrukh4's avatar

Hii @johnef_sh I think you can use Laravel's map() to add your custom key for the response like follows,

public function index() {
    $poLists = ( new PurchaseOrder() )
        ->where( 'needed_quantity', '>', 0 )
        ->where( 'supplier_id', $this->guard()->user()->id )
        ->get()
        ->groupBy( function ( $item ) {
              return [ $item->created_at->format( 'Y-m-d HH:mm:ss' ) ];
         });
    
    //This will create/transform your data
    $lists = $poLists->map(function($obj){
        $data = [
            'date' => $obj
        ];

        return $data;
    });

        return response()->json( $lists, 200 );
}
2 likes
johnef_sh's avatar

@SHAHRUKH4 - Thanks for replay, but still I have the same issue I need the timestamp to be the value of key date as now it becomes

{
    "2019-02-18 1212:0202:3939": {
        "date": [
            {
                "id": 2,
                "supplier_id": 15,
                "product_id": 857,
                "product_name": "بن test",
                "product_image": "1547638933.png",
                "needed_quantity": 5,
                "unit_price": "200.0000",
                "total_price": "1000.0000",
                "barcode": 654564,
                "status": 0,
                "created_at": "2019-02-18 12:16:39",
                "updated_at": "2019-02-18 12:16:39"
            }
        ]
    },

I need the timestamp to be the value of the key date something like this

"date" : 2019-02-18 1212:0202:3939 [
   {
       "id": 2,
       "supplier_id": 15,
   }
]
johnef_sh's avatar

@SHAHRUKH4 - Thanks again dear @shahrukh4 my laravel is 5.4 and php 5.6 but dingo/api uses laravel 5.5+ and needs PHP ^7.0 any other way to do that?

Shahrukh4's avatar

You don't have to pull in dingo api, you just need to use fractal transformer.

Please or to participate in this conversation.