i am developing a chat system for user and advisor,to show an inbox view where we get last message and profile image of the user, when we open it and then we get detailed chat of them, now to show an inbox view from lumen API where i send sentBy id of the user and all messages sent from that user will get accordingly, now the issue is if user sent message twice to the same user his object is also repeating but i want unique sentTo key in all json objects. i want to differentiate on the basis of sentTo key. Rest of the data remains the same but only sentTo key will be unique.
public function getConversationuser(Request $request)
{
$sentBy = $request->input('sentBy');
$sentBy = DB::table('chats')
->where('sentBy', '=', $sentBy)
->orderBy('chats.created_at','desc')
->get()->toArray();
return $sentBy;
}
it returns this json, i also tried distinct('sentTo') and groupBy('sentTo') but no luck
[
{
"id": 2,
"sentBy": 16,
"sentTo": 1,
"message": "toAdvisor",
"chatStatus": 0,
"time": "2019-01-16 14:42:55",
"senderType": null,
"created_at": "2019-01-16 09:42:55",
"updated_at": "2019-01-16 09:42:55"
},
{
"id": 1,
"sentBy": 16,
"sentTo": 1,
"message": "toAdvisor",
"chatStatus": 0,
"time": "2019-01-16 14:42:40",
"senderType": null,
"created_at": "2019-01-16 09:42:40",
"updated_at": "2019-01-16 09:42:40"
},
{
"id": 3,
"sentBy": 16,
"sentTo": 2,
"message": "toAdvisor",
"chatStatus": 0,
"time": "2019-01-16 14:42:40",
"senderType": null,
"created_at": "2019-01-16 09:42:40",
"updated_at": "2019-01-16 09:42:40"
}
]
but i want like
[
{
"id": 2,
"sentBy": 16,
"sentTo": 1,
"message": "toAdvisor",
"chatStatus": 0,
"time": "2019-01-16 14:42:55",
"senderType": null,
"created_at": "2019-01-16 09:42:55",
"updated_at": "2019-01-16 09:42:55"
},
{
"id": 3,
"sentBy": 16,
"sentTo": 2,
"message": "toAdvisor",
"chatStatus": 0,
"time": "2019-01-16 14:42:40",
"senderType": null,
"created_at": "2019-01-16 09:42:40",
"updated_at": "2019-01-16 09:42:40"
}
]