1

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"
        }
    ]
ahmad izhar
  • 150
  • 1
  • 13

1 Answers1

2

Remove toArray() so eloquent return by default a collection then you can apply :

$newArray= $sentBy->unique('sentTo');
Aziz.G
  • 3,599
  • 2
  • 17
  • 35
  • okay , you are right but i wont to differentiate on a single key item,as you can see there it doesn't work o single key which need to be differentiated. – ahmad izhar Jan 28 '19 at 07:35
  • remove toArray() to get a collection then use `$newArray= $sentBy->unique('sentTo');` – Aziz.G Jan 28 '19 at 15:14