F
Filament15mo ago
Vp

filter by relationship created at

How can I filter by relationship created_at column? My approach, which is not working
public function table(Table $table): Table
{
return $table
->query(Post::query()->with('tags'))
->columns([
TextColumn::make('title'),
TextColumn::make('tags.created_at')->date(),
TextColumn::make('created_at'),
])
->filters([
Filter::make('search')
->form([
DatePicker::make('tagDate')
->label('Tag Created'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['tagDate'],
fn (Builder $query, $date): Builder => $query->with([
'tags', fn ($q, $date) => $q->whereDate('created_at', $date),
]),
);
}),
], layout: FiltersLayout::AboveContent);
}
public function table(Table $table): Table
{
return $table
->query(Post::query()->with('tags'))
->columns([
TextColumn::make('title'),
TextColumn::make('tags.created_at')->date(),
TextColumn::make('created_at'),
])
->filters([
Filter::make('search')
->form([
DatePicker::make('tagDate')
->label('Tag Created'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['tagDate'],
fn (Builder $query, $date): Builder => $query->with([
'tags', fn ($q, $date) => $q->whereDate('created_at', $date),
]),
);
}),
], layout: FiltersLayout::AboveContent);
}
Solution:
Oh, I can use whereHas()..
Jump to solution
11 Replies
Solution
Vp
Vp15mo ago
Oh, I can use whereHas()..
Vp
VpOP15mo ago
Hi @Leandro Ferreira sorry for tag here, I've change my logic and it's working fine now but do you know why ->native(false) is not working inside filter->form()
LeandroFerreira
LeandroFerreira15mo ago
hum, I don't know
Vp
VpOP15mo ago
Sorry, I got typo.. everything is working fine now... haha
Helder Lima
Helder Lima14mo ago
Would be nice to see the solution for this, got exactly the same problem.
Vp
VpOP14mo ago
I've filter like this
->form([
DatePicker::make('tagDate'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['tagDate'],
fn (Builder $query, $date): Builder => $query->whereHas('tags',
function ($q) use ($date) {
return $q->whereDate('created_at', $date);
})
);
})
->form([
DatePicker::make('tagDate'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['tagDate'],
fn (Builder $query, $date): Builder => $query->whereHas('tags',
function ($q) use ($date) {
return $q->whereDate('created_at', $date);
})
);
})
Helder Lima
Helder Lima14mo ago
What relationship you got set between your models?
Vp
VpOP14mo ago
I thin i have belongto, i cannot check my code rn.. but any relationship should work
Helder Lima
Helder Lima14mo ago
I have a hasMany relationship and I want to filter by its create_at year, but filament doesnt filter out the records that doesnt not match the filter
Helder Lima
Helder Lima14mo ago
like so
No description
Want results from more Discord servers?
Add your server