How do I make a table column searchable based on its state value?
I have a table column that has a default value of a user_id. However, I format the state to show the users first and last name. Here is the code I have for doing so:
If I search using the user id, it works, however what I would like to be able to do is search by the value of the users name.
Is there a way to do this with my current code or do I need to do something differently?
8 Replies
Anyone able to help point me in the right direction for this? It would be much appreciated.
This might help you: https://filamentphp.com/docs/3.x/tables/columns/getting-started#searching
Especially this part:
Thank you Modestas, I will have a look at this now!
So this would work if I was searching on the contact resource but I am searching on a different resource where the default value is the id of the contact and not the name values.
This is why I use the formateStateUsing method and not sure how to make the above work with values from another table.
What exactly are you trying to achieve here? Are you loading the data (
first_name
+ last_name
) from a relationship? If yes, there are better ways to do this.
Either way, Modestas' advice still applies, you just have to change the query - which will be a bit of a custerfuck to make it work with your current table.It doesn't really matter if it's current or related table π What matters is the SQL query you will write. In your case, you have to specifically define the table/relationship you are searching in
My apologies for misunderstanding and potentially not explaining myself clearly enough.
My end goal is to be able to search for a clients name (first_name/last_name) on my project resource table.
What exactly are you trying to achieve here? Are you loading the data (first_name + last_name) from a relationship? If yes, there are better ways to do this.At the moment no, the
id
value of the client is returned and then I formateStateUsing
the id
to get the client details and render the name.
It doesn't really matter if it's current or related table π What matters is the SQL query you will write. In your case, you have to specifically define the table/relationship you are searching inI do have a relationship defined but I will assume it is wrong somewhere as it is not working correctly.
You should fix your relationship. Additionally, you can use a virtual column to define a
full_name
column
$table->string('full_name')->virtualAs('concat(first_name, \' \', last_name)');
I will look at this, thank you Leandro
This is working now and looks like this:
Thank you all