F
Filament5mo ago
Hespro

whereHas() query doesn't accept variables when filtering.

So here's my code:
Section::make('Stadijas')
->schema([
Wizard::make([
...$stages->map(function (ProjectStage $stage) {
return Wizard\Step::make($stage->name)
->schema([
Repeater::make("projectSubpoints")
->relationship('projectSubpoints', fn(Builder $query) => $query->whereHas('projectStageSubpoint', function (?Builder $query) use ($stage) {
$query?->where('project_stage_id', $stage->id);
}))
->schema([
Placeholder::make('Nosaukums')
->content(fn(?ProjectSubpoint $record) => $record?->projectStageSubpoint()?->first()?->name)
->label($stage->name),
Placeholder::make('Test')
->content(fn() => $stage->id),
Toggle::make('visible'),
Toggle::make('status'),
])->columns(3)
->addable(false)
->deletable(false)
->reorderableWithDragAndDrop(false)
->orderColumn('order_column')
]);
})
])
])
->visible(fn(?Project $record) => $record?->has_bis_lieta)
Section::make('Stadijas')
->schema([
Wizard::make([
...$stages->map(function (ProjectStage $stage) {
return Wizard\Step::make($stage->name)
->schema([
Repeater::make("projectSubpoints")
->relationship('projectSubpoints', fn(Builder $query) => $query->whereHas('projectStageSubpoint', function (?Builder $query) use ($stage) {
$query?->where('project_stage_id', $stage->id);
}))
->schema([
Placeholder::make('Nosaukums')
->content(fn(?ProjectSubpoint $record) => $record?->projectStageSubpoint()?->first()?->name)
->label($stage->name),
Placeholder::make('Test')
->content(fn() => $stage->id),
Toggle::make('visible'),
Toggle::make('status'),
])->columns(3)
->addable(false)
->deletable(false)
->reorderableWithDragAndDrop(false)
->orderColumn('order_column')
]);
})
])
])
->visible(fn(?Project $record) => $record?->has_bis_lieta)
The problem is in the $query?->where('project_stage_id', $stage->id);. When entering a static number in the $stage->id, it works. But when I input the variable (I know it's working, because in the PlaceholderTest it shows it correctly) it just shows all the subpoints from the last stage. This is a ManyToMany relationship. The main $record has a HasMany relationship called projectSubpoints and in that pivot model, it has the link to the subpoints called projectStageSubpoint.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server