У меня есть этот массив и я хочу получать только уникальные записи на основе ключа user_to
{
"status": true,
"result": {
"unread_chat": 1,
"chatlist": [
{
"id": 769,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 754,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 753,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 749,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 748,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 746,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 741,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 740,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 739,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 738,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 735,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 733,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 732,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 729,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 727,
"user_id": 211,
"user_to": 39,
"chats": "hello",
"is_read": false,
"created_at": "2018-07-21 17:36:13",
"online": false,
"unread_msg": 0
},
{
"id": 726,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 725,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": true,
"unread_msg": 0
},
{
"id": 679,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": true,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 678,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": true,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
{
"id": 677,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": true,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
}
]
}
}
Ниже приведен код, который я использовал.
$messages = UserAdvertiserMessages::where(['user_id' => $user_id])
->orderBy('created_at', 'desc')
->skip($offset * $limit)
->take($limit)
->get();
Если бы я использовал ->unique('user_to'), я получил бы правильные результаты, но он добавил index. Итак, код для получения правильных результатов
$messages = UserAdvertiserMessages::where(['user_id' => $user_id])
->orderBy('created_at', 'desc')
->skip($offset * $limit)
->take($limit)
->get()->unique('user_to');
Выход
{
"status": true,
"result": {
"unread_chat": 1,
"chatlist": {
"0": {
"id": 769,
"user_id": 211,
"user_to": 210,
"chats": "There?",
"is_read": false,
"created_at": "2018-07-21 23:31:37",
"online": false,
"unread_msg": 0
},
"1": {
"id": 754,
"user_id": 211,
"user_to": 226,
"chats": "yes",
"is_read": true,
"created_at": "2018-07-21 22:17:21",
"online": false,
"unread_msg": 0
},
"14": {
"id": 727,
"user_id": 211,
"user_to": 39,
"chats": "hello",
"is_read": false,
"created_at": "2018-07-21 17:36:13",
"online": false,
"unread_msg": 0
}
}
}
}
Вы пробовали: ['user_id' => $user_id, 'user_to' => $user_to]?
Попробуйте $messages->values()->all();
@LawrenceCherone: как видите, user_to может быть несколько, и у меня не было их идентификаторов.






попробуйте с этим отчетливый()
$messages = UserAdvertiserMessages::where(['user_id' => $user_id])
->orderBy('created_at', 'desc')
->skip($offset * $limit)
->take($limit)
->get()->distinct('user_to');
Ваш отрывок - JSON, на самом деле не говорит нам, что содержит $ messages или где он используется. Я могу предположить, что индекс списка чатов представляет $ messages. Ваш код должен работать нормально.