Вы не можете использовать несколько клавиш. unique
имеет только 2 параметра: $key
и $strict
.
/**
* Return only unique items from the collection.
*
* @param string|callable|null $key
* @param bool $strict
* @return static|\Illuminate\Support\Collection
*/
public function unique($key = null, $strict = false)
{
if (! is_null($key)) {
return parent::unique($key, $strict);
}
return new static(array_values($this->getDictionary()));
}
Вы можете передать свой собственный обратный вызов, чтобы определить уникальность элемента:
$messages = $messages->unique(function ($item) {
return $item['msg_from'] . $item['ad_id'];
});
Тест
array:10 [
0 => array:13 [
"id" => 1
"ad_id" => "2866527370"
"msg_from" => "Doe"
"msg_to" => "Andy"
"email" => "chet28@yahoo.com"
"show_email" => 1
"phone" => "+1333054551110"
"show_phone" => 1
"subject" => "Nesciunt sed temporibus nulla quis ducimus."
"message" => "Animi minus dolorem nostrum consectetur eos. Dolorum molestiae ea mollitia autem facilis numquam."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
1 => array:13 [
"id" => 2
"ad_id" => "2866576390"
"msg_from" => "Aliman"
"msg_to" => "Phil"
"email" => "konopelski.bianka@hotmail.com"
"show_email" => 1
"phone" => "+5618143199214"
"show_phone" => 1
"subject" => "Animi aut voluptas commodi quis."
"message" => "Quisquam similique expedita quos vitae exercitationem omnis nostrum. Nesciunt aut ut rerum ex rem."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
2 => array:13 [
"id" => 3
"ad_id" => "2866576390"
"msg_from" => "Doe"
"msg_to" => "Phil"
"email" => "shaina92@gmail.com"
"show_email" => 1
"phone" => "+8476237443387"
"show_phone" => 1
"subject" => "Et alias animi non vel repellendus."
"message" => "Dolor eaque cupiditate aut omnis. Sed aut voluptatibus cupiditate consequatur."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
3 => array:13 [
"id" => 4
"ad_id" => "2866576390"
"msg_from" => "Doe"
"msg_to" => "Natalia"
"email" => "gabrielle86@hotmail.com"
"show_email" => 1
"phone" => "+2733705925893"
"show_phone" => 1
"subject" => "Et earum et est voluptates voluptatibus rem."
"message" => "Voluptas eum aut qui aut ex qui ipsa. Illo ea cupiditate est sunt qui iusto ut qui."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
4 => array:13 [
"id" => 5
"ad_id" => "2866527370"
"msg_from" => "Aliman"
"msg_to" => "Andy"
"email" => "tracey.ledner@morar.com"
"show_email" => 1
"phone" => "+5870383075455"
"show_phone" => 1
"subject" => "Sunt numquam et voluptatem pariatur quibusdam sit."
"message" => "Ad possimus corrupti provident hic rem."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
5 => array:13 [
"id" => 6
"ad_id" => "2866576390"
"msg_from" => "John"
"msg_to" => "Natalia"
"email" => "emilie08@yahoo.com"
"show_email" => 1
"phone" => "+9398780932593"
"show_phone" => 1
"subject" => "Voluptates consectetur velit commodi deleniti."
"message" => "Excepturi iste molestiae error qui."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
6 => array:13 [
"id" => 7
"ad_id" => "2866576390"
"msg_from" => "Aliman"
"msg_to" => "Andy"
"email" => "reece.welch@yahoo.com"
"show_email" => 1
"phone" => "+4309027792891"
"show_phone" => 1
"subject" => "Dolor consequatur mollitia sed numquam cupiditate."
"message" => "Nisi alias non assumenda dolorem necessitatibus non."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
7 => array:13 [
"id" => 8
"ad_id" => "2866576390"
"msg_from" => "Aliman"
"msg_to" => "Phil"
"email" => "santina.mosciski@morissette.com"
"show_email" => 1
"phone" => "+7738160060411"
"show_phone" => 1
"subject" => "Iste at velit doloremque quas."
"message" => "Vel error consequuntur incidunt sed nam itaque ut."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
8 => array:13 [
"id" => 9
"ad_id" => "2866576390"
"msg_from" => "Aliman"
"msg_to" => "Natalia"
"email" => "kim.considine@gmail.com"
"show_email" => 1
"phone" => "+7742196820229"
"show_phone" => 1
"subject" => "Praesentium repellat quod omnis."
"message" => "Est et hic consequatur aut et omnis et. Sit perferendis et error eaque."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
9 => array:13 [
"id" => 10
"ad_id" => "2866527370"
"msg_from" => "Aliman"
"msg_to" => "Andy"
"email" => "lane38@conroy.com"
"show_email" => 1
"phone" => "+6448254479027"
"show_phone" => 1
"subject" => "Dolore velit aut perspiciatis."
"message" => "Eligendi et eos harum magni quo pariatur porro. Non non qui quae voluptatum et laboriosam reiciendis."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
]
Результат
array:5 [
0 => array:13 [
"id" => 1
"ad_id" => "2866527370"
"msg_from" => "Doe"
"msg_to" => "Andy"
"email" => "chet28@yahoo.com"
"show_email" => 1
"phone" => "+1333054551110"
"show_phone" => 1
"subject" => "Nesciunt sed temporibus nulla quis ducimus."
"message" => "Animi minus dolorem nostrum consectetur eos. Dolorum molestiae ea mollitia autem facilis numquam."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
1 => array:13 [
"id" => 2
"ad_id" => "2866576390"
"msg_from" => "Aliman"
"msg_to" => "Phil"
"email" => "konopelski.bianka@hotmail.com"
"show_email" => 1
"phone" => "+5618143199214"
"show_phone" => 1
"subject" => "Animi aut voluptas commodi quis."
"message" => "Quisquam similique expedita quos vitae exercitationem omnis nostrum. Nesciunt aut ut rerum ex rem."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
2 => array:13 [
"id" => 3
"ad_id" => "2866576390"
"msg_from" => "Doe"
"msg_to" => "Phil"
"email" => "shaina92@gmail.com"
"show_email" => 1
"phone" => "+8476237443387"
"show_phone" => 1
"subject" => "Et alias animi non vel repellendus."
"message" => "Dolor eaque cupiditate aut omnis. Sed aut voluptatibus cupiditate consequatur."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
3 => array:13 [
"id" => 5
"ad_id" => "2866527370"
"msg_from" => "Aliman"
"msg_to" => "Andy"
"email" => "tracey.ledner@morar.com"
"show_email" => 1
"phone" => "+5870383075455"
"show_phone" => 1
"subject" => "Sunt numquam et voluptatem pariatur quibusdam sit."
"message" => "Ad possimus corrupti provident hic rem."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
4 => array:13 [
"id" => 6
"ad_id" => "2866576390"
"msg_from" => "John"
"msg_to" => "Natalia"
"email" => "emilie08@yahoo.com"
"show_email" => 1
"phone" => "+9398780932593"
"show_phone" => 1
"subject" => "Voluptates consectetur velit commodi deleniti."
"message" => "Excepturi iste molestiae error qui."
"is_read" => 0
"created_at" => "2020-03-22 04:19:54"
"updated_at" => "2020-03-22 04:19:54"
]
]
Я рекомендую использовать DISTINCT
вместо unique
метод сбора .