Is there an error in `Filament\Table\Table::defaultSort()`...?đ¤
I was trying to pass a Closure to
defaultSort()
for the second argument, $direction
for a table that I have.
Here is the latest version of defaultSort()
in Filament\Tables\Table
:
Seems like passing $direction
to Str::lower()
when $direction
is a Closure causes an exception: mb_strtolower(): Argument #1 ($string) must be of type string, Closure given
Am I doing this wrong, or is this an error...? Seems like this is an error to me.16 Replies
If this is allowed to take a Closure, can anyone point me to an example that works...?
A closure works, but I think it still has to return âascâ or âdescâ to be applied to the query.
Would help if you provided your default sort code.
Thx....but can
Str::lower()
process a Closure...? (I'll test shortly...)
Here's my defaultSort()
code:
No, the string facade doesnât accept closures in any of its methods, as far as I know.
So...there's an error in the filament code...
It simply takes
$direction
and passes it to Str::lower()
....And, I just tested, anyway....
$direction
can be a string or a Closure...or null
....If youâre returning a string from the callback then it shouldnât matter right.
Wrong...see my screenshot.
I'm passing a simple Closure that returns a string...and get the same error.
It's exactly what's happening in
defaultSort()
....
I'm hoping I'm missing something....but I don't think I am. I think the Tinkerwell test that I ran (see screenshot) demonstrates this.
Here's the defaultSort()
code again:
The question though, is why is your direction callback not passing a string of asc or desc, not that it isnât a bug or youâre doing anything wrong.
It is...it's returning either 'asc' or 'desc'....
Or, do you mean, why isn't it type-hinting a string...?
I'll try that, but I imagine it would result in the same thing...as demonstrated in the Tinkerwell test screenshot.
Here's a reference to my code...with the callback returning one string or the other....
This definitely looks like a bug. Thereâs nothing evaluating $direction if itâs a closure. And laravelâs Str::lower() doesnât accept a closure.
Please submit an issue on GitHub.
Thx for confirming my suspicions. đ
I'll submit an issue...perhaps submitting a PR over the weekend, time permitting. đ¤
Thank you.
GitHub
Now allowing defaultSort()'s argument to be a Closure. by telkins ...
The Filament\Tables\Table\Concerns\CanSortRecords trait's defaultSort() method has the following signature:
public function defaultSort(string | Closure | null $column, string | Closure | null ...
Feedback welcome/appreciated... đ¤