I have a query that looks like this:
$messages = Conversation::leftJoin('v_sendermessages', 'v_conversations.con_id', '=', 'v_sendermessages.sm_c_id')
->leftJoin('v_recipientmessages', 'v_conversations.con_id', '=', 'v_recipientmessages.rm_c_id')
->where('con_id', $conId)
->select('v_conversations.sender_id', 'v_conversations.recipient_id', 'v_sendermessages.sender_message', 'v_sendermessages.date_sent', 'v_recipientmessages.recipient_message', 'v_recipientmessages.date_answered')
->get();
and the output will be:
{
"messages": [
{
"sender_id": 2,
"recipient_id": 1,
"sender_message": "Asdfasdfasdf",
"date_sent": 1477664894,
"recipient_message": "Sdfsdfsdf",
"date_answered": 1477665242
},
{
"sender_id": 2,
"recipient_id": 1,
"sender_message": "Asdfasdfasdf",
"date_sent": 1477664894,
"recipient_message": "Ju",
"date_answered": 1477665442
},
{
"sender_id": 2,
"recipient_id": 1,
"sender_message": "Asdfasdfasdf",
"date_sent": 1477664894,
"recipient_message": "Sdafasdf",
"date_answered": 1477666240
}
]
}
But how can I modify the above query to output json that looks like this:
{
"senderMessages": [
{
*sender messages listed here*
}],
"recipientMessages": [
{
*recipient messages listed here*
]}
I use this query to show a conversation, so it would be much easier to list all sender messages in one section and all recipient in another section
json_encode($messages);