Search in morph to many relationships
I have a Payment Resource. In the payment model, I have a morph to relationship with payee
In the table, I have a column in which I show the code of the payee. The code is like this
I want to make this column searchable. Is there a way to achieve it?
I can only think of joining the tables in the Eloquent query and change the column definition using a case statement. But that is rather messy and would like to have a cleaner solution, if available.
Thanks in advance for any help.
16 Replies
I think you have to use
getStateUsing
instead of formatStateUsing
to make it searchable.Nope, gives server error.
What is the error? My guess is that the relationship isn’t getting eager loaded.
Stack Trace
May be I am doing something wrong. I have replaced formatStateUsing by getStateUsing. Not even added ->searchable().
This itself is giving server error.
Try this:
$state won’t be an instance of the model. It will be an array. That’s why it’s a dependency injection error.
Unfortunately this does not work. Only this shows the table correctly. Now I added searchable and when I tried to search again gives server error.
hmm - the last thing that pops to my mind would be enhancing the
getTableQuery()
-function with with(['payee'])
and try again.This also does not work.
However, if I just do
This seems to work only for the Candidate model. So, I am splitting the column into two : registration_code and roll_no. Then it works separately on each models.
I just read the docs again - and you can actually do a Closure in the searchable-function! (see https://filamentphp.com/docs/2.x/tables/columns/getting-started#searching ) -> so you could do that and be like "whereHas()" and subquery that...
Filament
Getting started - Columns - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
Nope! Works for only one.
aaaaah I don't think so... 😁
That's great. I will try it out.
or something along those lines should do the trick ^^
I would actually add a column to the main-model to cercumvent that morphTo-Cluster-F cause even tho it is possible to build a search-query, it will be complete horse-💩 performance-wise... 😄
This is what finally I come up with which works
Thanks for the tip. I will try it.
Yay - good luck! 🙂