How to filter table based on grandparent relationships?
Say I have relationship structure: Project > Category > Article > Illustration
I'm interested in filtering by Project when viewing the Illustration list.
I have set up all my
I also realized that you can do some filament stuff for Form: Selects in forms like this: That admittedly works for forms, but I cannot seem to find a similar / suitable solution for filtering table data based on grandparent relationships. (I also experimented with the
belongsTo()
relationships. It's simple when using pure Laravel; to traverse back up the parents:
But in filament I'm not sure how to handle this type of filtering. I realize this would be easy if Laravel handled nested relationships but apparently it does not π¦I also realized that you can do some filament stuff for Form: Selects in forms like this: That admittedly works for forms, but I cannot seem to find a similar / suitable solution for filtering table data based on grandparent relationships. (I also experimented with the
belongs-to-through
package, but that doesn't seem to work either)
Any help would be appreciated, thanks!10 Replies
Have you tried custom filters?
Filament
Filters - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
You might try deep query, maybe even join to select relevant results based on your preferences.
I guess you might achieve what you want with JOINs. π
I saw that but didn't think it would apply. I don't see anything in the laravel docs for "deep query" but I'll look into joins
I think I have made a little progress but I'm not sure what to make of this. My filter now looks like this:
And I'm attaching the dropdown result here.
The join is giving me a category for all of the articles. I think this is a cartesion product? or something but either way...I'm not sure how to proceed. btw: I'm looking to filter based on
projects
but I'm try to take baby steps here πYou could use some packages to make a deep select
which ones? I tried the staudenmeir packages and don't believe they work for simple belongsto relationships. Please correct me if I'm wrong or provide some further examples if possible, thanks!
His packages work and they are great. If I remember correctly you need to specify relation in the model using his package. But don't know now in your case when you want so deep
Not sure what brain farting was happening, but now I got this working with the staudenmeir package. And yes, the solution looks very satisfying! I was accidentally using the
belongs to many deep
. Here is my working code for any future travelers (not that they will find it in this discord mess )
I can't believe how elegant and simple the solution is. Note: I have only done superficial testing and so far it seems that this indeed works. Thank you @krekas for righting my ship headed in the wrong direction