Home > Blockchain >  Convert json data in simple form - Laravel chat-inbox
Convert json data in simple form - Laravel chat-inbox

Time:01-15

I am trying to get latest messages from table 'messages' for inbox The chat is between user and owner of the item (listed on my portal) I want to get unique data with item_id

My code is

public function inbox()
{
    // $inbox = ChatMessage::distinct('item_id')->where('incoming_msg_id', auth()->user()->id)->orWhere('outgoing_msg_id', auth()->user()->id)->orderBy('msg_id', 'DESC')->get();


    $inbox = Message::where('incoming_msg_id', auth()->user()->id)->orWhere('outgoing_msg_id', auth()->user()->id)->latest('msg_id')->get()->unique('item_id');


    return $this->sendResponse($inbox,'Chat');
}

Result is perfect just i need is non-associative/non-object array.

Current output

{
"success": true,
    "data": {
        "0": {
            "msg_id": 68,
            "incoming_msg_id": 0,
            "outgoing_msg_id": 2,
            "msg": "ok let me check",
            "msg_for": "hadiya",
            "item_id": "13",
            "msg_at": "1 day ago"
        },
        "1": {
            "msg_id": 62,
            "incoming_msg_id": 0,
            "outgoing_msg_id": 2,
            "msg": "test msg",
            "msg_for": "hadiya",
            "item_id": "1",
            "msg_at": "2 days ago"
        }
    },
    "message": "Chat"
}

Expected Output

{
"success": true,
    "data": [
        {
            "msg_id": 68,
            "incoming_msg_id": 0,
            "outgoing_msg_id": 2,
            "msg": "ok let me check",
            "msg_for": "hadiya",
            "item_id": "13",
            "msg_at": "1 day ago"
        },
        {
            "msg_id": 62,
            "incoming_msg_id": 0,
            "outgoing_msg_id": 2,
            "msg": "test msg",
            "msg_for": "hadiya",
            "item_id": "1",
            "msg_at": "2 days ago"
        }
    ],
    "message": "Chat"
}

Thanks

CodePudding user response:

I guess you could convert them to json before sending it as the response:

return $this->sendResponse($inbox->toJson(),'Chat');

CodePudding user response:

It's simple by following approach you can set the http status code also (optional).

return response()->json(['chat' => $inbox], 200);
  •  Tags:  
  • Related