inertia icon indicating copy to clipboard operation
inertia copied to clipboard

Eloquent sortBy() method not working when sending response to frontend

Open Arturexe opened this issue 2 years ago • 3 comments

Versions:

"@inertiajs/inertia": "^0.11.0",
"@inertiajs/inertia-vue3": "^0.6.0",

Steps to reproduce:

When I sort my model with sortBy/sortByDesc to send it to the frontend:

$events = Events::all()->sortByDesc('date');

\Log::info($events);

return inertia('events', compact('events'));

log:

local.INFO: {
"5":{"id":7,"date":"2022-07-30"},
"0":{"id":1,"date":"2022-07-22"},
"1":{"id":2,"date":"2022-07-22"},
"2":{"id":3,"date":"2022-07-22"},
"3":{"id":5,"date":"2022-07-22"},
"4":{"id":6,"date":"2022-07-05"}
}

I am receiving an object, that is not sorted:

const props = defineProps({
    events: Object
})

console.log(props.events)

console (abrev.):

0: {id: 1, date: "2022-07-22"}
1: {id: 2, date: "2022-07-22"}
2: {id: 3, date: "2022-07-22"}
3: {id: 5, date: "2022-07-22"}
4: {id: 6, date: "2022-07-05"}
5: {id: 7, date: "2022-07-30"}

When I'm using Model::orderBy('date', 'desc')->get() instead, everything works fine and the object arrives in the frontend sorted. Either I'm doing something wrong, or there's a bug.

Arturexe avatar Jul 22 '22 08:07 Arturexe

i want to say this might still be a laravel idiosyncrasy. I had a similar problem without inertia, but i can't remember the details. something to do with all() vs get() or orderBy vs sortBy and/or maybe the combo with compact(). Maybe try outputting to a laravel blade view to rule laravel issue out?

ITwrx avatar Jul 26 '22 17:07 ITwrx

@Arturexe, this will not converted to an array in Javascript. That's an object.

local.INFO: {
  "5":{"id":7,"date":"2022-07-30"},
  "0":{"id":1,"date":"2022-07-22"},
  "1":{"id":2,"date":"2022-07-22"},
  "2":{"id":3,"date":"2022-07-22"},
  "3":{"id":5,"date":"2022-07-22"},
  "4":{"id":6,"date":"2022-07-05"}
}

The indices are converted to properties in the object.

2 solutions:

1 order in the database:

$events = Events::orderByDesc('date')->get();

2 the the numeric keys in order so it will be interpreted by Javascript as an array

$events = Events::all()->sortByDesc('date')->values();

ordago avatar Jul 27 '22 13:07 ordago

oh, yeah, that's ringing a bell...

ITwrx avatar Jul 27 '22 17:07 ITwrx

Hey! Thanks so much for your interest in Inertia.js and for sharing this issue/suggestion.

In an attempt to get on top of the issues and pull requests on this project I am going through all the older issues and PRs and closing them, as there's a decent chance that they have since been resolved or are simply not relevant any longer. My hope is that with a "clean slate" me and the other project maintainers will be able to better keep on top of issues and PRs moving forward.

Of course there's a chance that this issue is still relevant, and if that's the case feel free to simply submit a new issue. The only thing I ask is that you please include a super minimal reproduction of the issue as a Git repo. This makes it much easier for us to reproduce things on our end and ultimately fix it.

Really not trying to be dismissive here, I just need to find a way to get this project back into a state that I am able to maintain it. Hope that makes sense! ❤️

reinink avatar Jul 28 '23 01:07 reinink