F
Filamentβ€’2y ago
bionary

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 belongsTo() relationships. It's simple when using pure Laravel; to traverse back up the parents:
$illustration->article->category->project;

//works great!
$illustration->article->category->project;

//works great!
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:
Group::make([
Group::make([
Group::make([
Select::make('project_id')
->relationship('project','name')
->reactive()
->required()
])->relationship('category'),
])->relationship('article'),
]),
Group::make([
Group::make([
Group::make([
Select::make('project_id')
->relationship('project','name')
->reactive()
->required()
])->relationship('category'),
])->relationship('article'),
]),
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
Crylar
Crylarβ€’2y ago
Have you tried custom filters?
Crylar
Crylarβ€’2y ago
Crylar
Crylarβ€’2y ago
You might try deep query, maybe even join to select relevant results based on your preferences.
Crylar
Crylarβ€’2y ago
I guess you might achieve what you want with JOINs. πŸ™‚
bionary
bionaryOPβ€’2y ago
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
bionary
bionaryOPβ€’2y ago
I think I have made a little progress but I'm not sure what to make of this. My filter now looks like this:
SelectFilter::make('Cat')->relationship('article', 'path',
fn (Builder $query) => $query
->join('categories', 'categories.id', '=', 'articles.category_id')
),
SelectFilter::make('Cat')->relationship('article', 'path',
fn (Builder $query) => $query
->join('categories', 'categories.id', '=', 'articles.category_id')
),
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 πŸ˜‰
krekas
krekasβ€’2y ago
You could use some packages to make a deep select
bionary
bionaryOPβ€’2y ago
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!
krekas
krekasβ€’2y ago
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
bionary
bionaryOPβ€’2y ago
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 )
///Illustration model
public function project() {
return $this->belongsToThrough(Project::class, [Category::class, Article::class]);
}

//resource filter
SelectFilter::make('Project')->relationship('project', 'name'),
///Illustration model
public function project() {
return $this->belongsToThrough(Project::class, [Category::class, Article::class]);
}

//resource filter
SelectFilter::make('Project')->relationship('project', 'name'),
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
Want results from more Discord servers?
Add your server