-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collection sortBy does not work if collection elements are stdClass #35947
Comments
Your example Instead call $a = new stdClass;
$a->name = 'a';
$b = new stdClass;
$b->name = 'b';
$c = new stdClass;
$c->name = 'c';
collect([$b, $a, $c])->sortBy('name');
// => Illuminate\Support\Collection {
// all: [
// 1 => {
// "name": "a",
// },
// 0 => {
// "name": "b",
// },
// 2 => {
// "name": "c",
// },
// ],
// }
collect([$b, $a, $c])->sortByDesc('name');
// => Illuminate\Support\Collection {
// all: [
// 2 => {#3419
// "name": "c",
// },
// 0 => {
// "name": "b",
// },
// 1 => {
// "name": "a",
// },
// ],
// } If you have a look at the framework's call stack for
$c = new stdClass;
$c->name = 'c';
data_get($c, 'name');
// => "c" |
@derekmd scroll down a bit further in the documentation you linked and you will see that what I said should work the way I proposed according to the 8.x documentation. This was added in 8.x it appears as this section does not appear in 7.x or lower It comes under the paragraph that starts with:
Here's a screenshot from the link you sent me. I highlighted the bit that shows it supports the wordage of 'asc' and 'desc' This functionality was added by Taylor in 53eb307 Considering the sortByMany function was taken directly from Arr I'd say it was written in a way that only supports arrays. While instead it should be supporting data_get. I don't see it using data_get at all here |
This fixes laravel#35947. Since collections can deal with objects then the method sortByMany taken from Arr should be able to deal with objects as well
I added a fix in the linked PR against the function |
Ah sorry, I was mistakenly looking at a stale 7.x branch on my local machine and I didn't realize multi-sort was added by 8.x. The original This (less pretty) arrow function syntax also works to handle collect([$b, $a, $c])->sortBy([
fn($a, $b) => strcmp($a->name, $b->name), // name asc
fn($a, $b) => $b->id - $a->id, // id desc
]); |
Thanks @derekmd. I thought about doing that as well. However I'm being told by @GrahamCampbell that Another solution for me would be to just use Eloquent in this instance 🤷♂️ |
Description:
When running raw database queries through illuminate the returned response is a collection of stdClass objects (This is because the default fetch mode for PDO is set to PDO::FETCH_OBJ). I'm not sure if this is the intended result but these objects can't be sorted because stdClass does not implement the ArrayAccess interface. Thus when Laravel Collections tries to sort the collection all results end up returning null and it's unsortable.
I would say this is a non issue at first but because any queries return a collection one would think it should work. I assume other methods from collections will have issues from any raw results as well.
Steps To Reproduce:
Output:
A "quick" fix online is to just cast all stdClass to array through map then sort:
Output:
This does indeed correct the issue but it seems to over complicate something that I think collections could detect. That being the usage of stdClass which can be temporarily converted to perform the desired functions.
The text was updated successfully, but these errors were encountered: