F
Filament4mo ago
Loxxer

How to disable scopes for related model?

I can't get it to work. I'm building a table in ProductResource using
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn (Builder $query) => $query->withoutGlobalScopes())
->recordClasses(fn (Model $record) => match ($record->local_status) {
'hidden' => 'opacity-50',
default => null,
})
->recordClasses(fn (Model $record) => match ($record->operation_status) {
'discontinued' => 'opacity-50', //grayscale
default => null,
})
->columns([
Tables\Columns\TextColumn::make('id')
->sortable()
->searchable()
->copyable()

Tables\Columns\ImageColumn::make('brand.icon')
->size(20)
->label('Brand')

])
...
}

public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn (Builder $query) => $query->withoutGlobalScopes())
->recordClasses(fn (Model $record) => match ($record->local_status) {
'hidden' => 'opacity-50',
default => null,
})
->recordClasses(fn (Model $record) => match ($record->operation_status) {
'discontinued' => 'opacity-50', //grayscale
default => null,
})
->columns([
Tables\Columns\TextColumn::make('id')
->sortable()
->searchable()
->copyable()

Tables\Columns\ImageColumn::make('brand.icon')
->size(20)
->label('Brand')

])
...
}


I've deactivated global sopes using modifyQueryUsing(), but this only works for the global scopes for the 'Product' model. As you can see, I am creating an ImageColumn from the related model 'Brand'. How can I disable global scopes for this relation? Right now, it's only creating an ImageColumn for scoped brands, but I need images for all brands, independent of the global scopes of the 'Brand' model. How can I managecan I manage this?
2 Replies
toeknee
toeknee4mo ago
You need to define which scope you don't want. i.e.
->withoutGlobalScope('App\Models\Scopes\HasTeamScope')
->withoutGlobalScope('App\Models\Scopes\HasTeamScope')
Loxxer
Loxxer4mo ago
Unfortunately, this does not work at all and limits the results even more. If I define a scope as you describe, the defined scope is strangely applied directly to the scope-related product models. In
Tables\Columns\ImageColumn::make('brand.icon')
->size(20)
->label('Brand')
Tables\Columns\ImageColumn::make('brand.icon')
->size(20)
->label('Brand')
the related 'Brand' model is queried. Unfortunately, the scope for the model 'Brand' is used in this query and no icons are displayed if the brand is set to 'discontinued' (the scope ensures that only brands that are 'in_service' are displayed ). I need something like modifyRelationQueryUsing():
Tables\Columns\ImageColumn::make('brand.icon')
->modifyRelationQueryUsing(fn (Builder $query) => $query->withoutGlobalScopes()) // <- here...!
->size(20)
->label('Brand')
Tables\Columns\ImageColumn::make('brand.icon')
->modifyRelationQueryUsing(fn (Builder $query) => $query->withoutGlobalScopes()) // <- here...!
->size(20)
->label('Brand')
Is there a solution for that or a quick workaround?