BelongsTo is returning all records - ignoring where on relationship?

I have a Task Model with this relationship
public function taskStatuses() : BelongsTo
{
return $this->belongsTo(Status::class, 'status_id')->where('model','Task');
}
public function taskStatuses() : BelongsTo
{
return $this->belongsTo(Status::class, 'status_id')->where('model','Task');
}
On my form i have a select
Forms\Components\Select::make('status_id')
->relationship('taskStatuses', 'name')
->required(),
Forms\Components\Select::make('status_id')
->relationship('taskStatuses', 'name')
->required(),
but its returning all task statuses - not only the ones where the model column = Task? am i doing something wrong here? Thanks for any advice!
5 Replies
Dan Harrin
Dan Harrin2y ago
yeah this is just how filament works atm when it gets the relationship query laravel strips the sceope its a bit weird might try and fix it in v3 but not sure how well it will work
Patrick Boivin
A workaround I've been using is to fill the options and validate manually:
Forms\Components\Select::make('status_id')
->options($options = Status::where('model','Task')->pluck('name', 'id'))
->rule(Rule::in($options->keys()))
->required(),
Forms\Components\Select::make('status_id')
->options($options = Status::where('model','Task')->pluck('name', 'id'))
->rule(Rule::in($options->keys()))
->required(),
nostrodamned
nostrodamnedOP2y ago
You Sire ar a genius! I have been going nuts with this all day!
LeandroFerreira
//Illuminate\Database\Eloquent\Builder
->relationship('taskStatuses', 'name', fn (Builder $query): Builder => $query->where('model', 'Task'))
//Illuminate\Database\Eloquent\Builder
->relationship('taskStatuses', 'name', fn (Builder $query): Builder => $query->where('model', 'Task'))
?
Patrick Boivin
A bit cleaner I think! But you would still need the validation. Or would you? lol, I don't even know.
Want results from more Discord servers?
Add your server