Is it possible to sort a table by a relation field...? π€
I have a table that displays grouped data. The data is grouped by a (date/time) field on one of the resource's relations, which works great.
But, I would like to sort the table on this same field.
It doesn't seem to be possible....unless I've missed something...? π³
Does anyone know whether or not this is possible...? π€
11 Replies
Hmmm...looking for something else in the docs, I think I may have found the answer.... π€
Well, this is what I found: https://filamentphp.com/docs/3.x/tables/grouping#customizing-the-eloquent-query-ordering-behaviour
But, I can't seem to figure out whether or not this is to be used for what I'm trying to do. In any case, I'm still stuck.... π’
does defaultSort not work?
No, it doesn't. I tried the following:
Remember, I'm using the grouping functionality and trying to get it to sort by the same relation field that it's grouping on.
I get an SQL error:
Column not found: 1054 Unknown column 'relation.date_time' in 'order clause'
It seems to add the relationship column in the main query incorrectly. (It could be a bug, but I suspect I'm doing something wrong, instead.)
The query it tries to run:
@travis.elkins i honestly havent had luck sorting on any relational field. That sort that you are trying to do is based on sortable() on the TextColumn, etc. Default sort isnt the issue, just sortable in general with a relationship
I'm not sure what you mean by it being "based on sortable() on the TextColumn, etc." I'm calling
$table->defaultSort()
.... π€
But, if there are problems, in general, with sorting on relationship fields, then it may not matter what I do or where....right, but thats not really based on the query, its based on the column if that makes any sense. If you remove that default and set sortable() on that column and then manually try to sort it by click on the header, it will give you that exact same error
maybe it doesnt work exactly that way, but definitely handles it similarly and yields the same result
So...is this a bug...? I don't want to spend any more time trying to figure it out if it's a lost cause. And, while I've explored some of the code, there's a lot going on that I don't quite understand.
more a limitation imho. Its been that way the for the last 2 years. I simply disable sorting on those columns. For your need, off the top of my head, i am not sure the best solution. Might have to be in your initial query, but obvously that wont be changeable then.
Thx, @Mark Chaney . Appreciate the input/help....
@travis.elkins we are discussing the searchable() aspects of it here https://discord.com/channels/883083792112300104/1152292575521288212, which could still pertain to yours as well. Obviously no solution yet, but hopefully we can come up with an improvement.
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.