Sort by pivot date
I'm in my Songs table
I have this column
but when i sort it, it doesnt crash, but i dont see the dates sorted
i have 3 tables
song: id, name
user_song_play_history: song_id, user_id, played_at
user: id, name
is there any way to achieve this sorting?
i also tried:
Solution:Jump to solution
```PHP
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->select('songs.*', DB::raw('(select played_at from user_song_play_history where songs.id = song_id order by played_at desc limit 1) as latest_played_at'));...
24 Replies
bump
bump
Hi Eric, I tried it... I see that the ->latestOfMany is the cullprit
If you change your function to this it will work
maybe there is a more elegant solution? But for the time being this works π
thanks for ur answer! @Tally but hmm im gettin this when i click the sort icon in the table now
to try to solve it i modified the fn once again, now trying it this way:
but it still doesnt work as i expect
for example:
querystring:
/songs?tableSortColumn=latestSongHistory.played_at&tableSortDirection=desc
but the dates arent sorted properly by the last time listened date field
/songs?tableSortColumn=latestSongHistory.played_at&tableSortDirection=asc
and asc seems it doesnt neitherSomething is off in your relationship. It canβt be a HasOne with a latest of many.
why not?
Yeah, why not? π
hmmm must be possible to add an order and a limit to the order by subquery
how that could be achieved?
u mean modifying somehow the query u provided?
not sure why in the error
limit 10
is shown when limit is set to 1
It's difficult debugging the actual code without a testrepo
The limit 10 is outside of the subquery (it's the default table limit)
oh okay yeah makes sense
so, if i create a test repo, would u mind checking it out?
ill set it all up ez to reproduce
yeah no problem
perfect, i guess today afternoon ill be able to reproduce it in a repo, then ill ping u π
thanks again
@Tally i finally was able to create the repo
https://github.com/ericmp33/filatest/blob/main/README.md
when u up to just let me know π
shouldn't be so difficult you would think...
ended up with this to get it working
Solution
maybe something for a real Filament Pro dev π
no way!
seems it works, im implementing it
pfff yeah
so u went to the raw sql
nice
It'll work... but it's not the Filament way... had something to do with the way Filament/Laravel does the ordering with related fields
π
If someone else has a nice idea you can always refactor π good luck mate
oh i see, well the trick u did is valid to me (:
im wondering if i can somehow convert the query to a virtual column. ill search that. it may be an improvement, not sure though. gotta search how sql virtual columns actually work and how capable are to do this type of stuff
yeah absolutely, thanks a lot appreciate the time u spent on this π