F
Filament8mo ago
Fogzy

Global Scopes not being ignored on select

I have this select:
Select::make('categories')
->relationship('categories', 'title', function (Builder $query): Builder {
return $query->withoutGlobalScopes();
})
->multiple()
->searchable()
->preload()
->required()
Select::make('categories')
->relationship('categories', 'title', function (Builder $query): Builder {
return $query->withoutGlobalScopes();
})
->multiple()
->searchable()
->preload()
->required()
And everything works fine until I save. Then the options that are displayed are not complete (it's obvious that the global scope is applied). I have global scopes disabled for the whole resource:
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->withoutGlobalScopes();
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->withoutGlobalScopes();
}
The DB shows that the selected options got saved (4 rows). But the select only shows 3 options selected (since the 4th is blocked by the scope).
2 Replies
toeknee
toeknee8mo ago
Try disabling globalscopes and setting them: return parent::getEloquentQuery()->withoutGlobalScopes(['scope1', 'scope2']);
Fogzy
FogzyOP8mo ago
@toeknee I don't think that's any different from what I already have?
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->withoutGlobalScopes();
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->withoutGlobalScopes();
}
But anyway, tried specifying the specific scopes and still no luck Had to add this to get it working:
->afterStateHydrated(function (Select $component): void {
/** @var Product|null $product */
$product = $component->getRecord();

if (! $product) {
return;
}

$ids = DB::table('category_product')
->select('category_id')
->where('product_id', $product->id)
->pluck('category_id');
$component->state($ids);
})
->afterStateHydrated(function (Select $component): void {
/** @var Product|null $product */
$product = $component->getRecord();

if (! $product) {
return;
}

$ids = DB::table('category_product')
->select('category_id')
->where('product_id', $product->id)
->pluck('category_id');
$component->state($ids);
})
Want results from more Discord servers?
Add your server