Reputation: 1114
I have a collection which I have been able to sort to return to me sorted by user id and by date, the problem being that I need to get a unique user with the most current date, but I can't get it. This is my json:
{
"168": {
"created_at": "2019-01-23 12:40:26",
"user_id": 1,
"from": "form",
"name": "Pedro",
"lastname": "Pretgar",
"email": "[email protected]"
},
"154": {
"created_at": "2019-06-28 13:34:21",
"user_id": 1,
"from": "form",
"name": "Pedro",
"lastname": "Pretgar",
"email": "[email protected]"
},
"167": {
"created_at": "2019-01-31 17:33:26",
"user_id": 8,
"from": "gmail",
"name": "Lilian",
"lastname": "Houster",
"email": "[email protected]"
},
"156": {
"created_at": "2019-06-08 12:14:42",
"user_id": 14,
"from": "form",
"name": "Jhon",
"lastname": "Cobra",
"email": "[email protected]"
},
"38": {
"created_at": "2019-07-21 21:22:18",
"user_id": 14,
"from": "form",
"name": "Jhon",
"lastname": "Cobra",
"email": "[email protected]"
},
"81": {
"created_at": "2019-09-25 10:24:34",
"user_id": 14,
"from": "form",
"name": "Jhon",
"lastname": "Cobra",
"email": "[email protected]"
}
}
I need this result:
{
"154": {
"created_at": "2019-06-28 13:34:21",
"user_id": 1,
"from": "form",
"name": "Pedro",
"lastname": "Pretgar",
"email": "[email protected]"
},
"167": {
"created_at": "2019-01-31 17:33:26",
"user_id": 8,
"from": "gmail",
"name": "Lilian",
"lastname": "Houster",
"email": "[email protected]"
},
"81": {
"created_at": "2019-09-25 10:24:34",
"user_id": 14,
"from": "form",
"name": "Jhon",
"lastname": "Cobra",
"email": "[email protected]"
}
}
I have tried, sorting by date and then getting the unique value, the problem is that I don't know how to sort first by the most current date and then get the unique value.
$tmp = $users->sortBy(function($user) {
return [$user->user_id, $user->created_at];
})->unique('user_id');
Upvotes: 0
Views: 1977
Reputation: 155
you can try
$users->orderBy('created_at', 'desc')
->orderBy('user_id', 'asc')
->groupBy('user_id')
->get();
Upvotes: 0
Reputation: 597
This code will works :
$users
->sortByDesc('created_at')
->unique('user_id');
Sort by desc method : https://laravel.com/docs/7.x/collections#method-sortbydesc
Unique method : https://laravel.com/docs/7.x/collections#method-unique
It will return this array :
81 => { #9 ▼
+"created_at": "2019-09-25 10:24:34"
+"user_id": 14
+"from": "form"
+"name": "Jhon"
+"lastname": "Cobra"
+"email": "[email protected]"
}
154 => {#5 ▼
+"created_at": "2019-06-28 13:34:21"
+"user_id": 1
+"from": "form"
+"name": "Pedro"
+"lastname": "Pretgar"
+"email": "[email protected]"
}
167 => {#6 ▼
+"created_at": "2019-01-31 17:33:26"
+"user_id": 8
+"from": "gmail"
+"name": "Lilian"
+"lastname": "Houster"
+"email": "[email protected]"
}
If you want the exact array as you have, sorted by the most recent date and the user_id I guess, you can chain with another sort method with the user_id :
$users
->sortByDesc('created_at')
->sortBy('user_id')
->unique('user_id');
It will return this array :
154 => {#5 ▼
+"created_at": "2019-06-28 13:34:21"
+"user_id": 1
+"from": "form"
+"name": "Pedro"
+"lastname": "Pretgar"
+"email": "[email protected]"
}
167 => {#6 ▼
+"created_at": "2019-01-31 17:33:26"
+"user_id": 8
+"from": "gmail"
+"name": "Lilian"
+"lastname": "Houster"
+"email": "[email protected]"
}
81 => {#9 ▼
+"created_at": "2019-09-25 10:24:34"
+"user_id": 14
+"from": "form"
+"name": "Jhon"
+"lastname": "Cobra"
+"email": "[email protected]"
}
Upvotes: 3