How to make custom datetime column sortable?
im in my songs table. i want to sort the songs by the last time i played them
i have a pivot table between songs and users, there i store the
played_at
field, along with other relevant pivot attributes
so far i have this code:
seems i have to join the tables somehow and then just sort by that field. the problem is that when i join them i get and idk how to solve it. to solve it i tried to select only the columns i needed from the pivot table, so i dont get the created_at field, but i couldnt get to do it:
i also tried it this way but it doesnt work:
17 Replies
if you define the realationship in your model .. you don't have to do anything in table
oh
okay i added the relationship & did this:
but then i sort it and the records order is the same, they dont get sorted
what im missing?
at the moment i added the
->dateTime()
just in case
but nothing
btw didnt know about latestOfMany
@mvenghaus thanks for that! every day learning new stuff ^^yeah .. that's very powerful
i would look at the query in debugbar .. maybe there is another sort in front
with this filtering is also easy
hmm okay, using debugbar i get:
at first sight, u see it fine? idk š¤
hmmm, gotta try that too!
do you have default sort on songs for created_at ?
should be not default .. should be hard in a query
played_at is on second position
oh god
must be that 100%
cant check it right now but will do soon and i let u know
@mvenghaus it worked, yeah i had a
->latest()
(,:
btw how would u use ->whereRelation in this case?this is a filter of mine .. very similar
is this L11?
or in L10 is available too?
10
oh okay
long time
is like using whereHas, right?
similar i guess
yeah .. but less code and more beautiful
š
yes!
^^
thanks again š
you're welcome
one last note .. wherehas is more modifiing the second select for data .. latestofmany uses a join .. that's why you can filter with whereRelation
so u mean that with latestofmany i can filter and use whererelation
and wherehas only modifies the second select's data?
to be honest .. i'm not completly sure .. never tested latestofmany with wherehas š but that's how i use it