F
Filament5mo ago
pjb

withoutGlobalScope() with Column Relationships

Let's a say I have a table column that uses a relationship, like this example from the docs: https://filamentphp.com/docs/3.x/tables/columns/relationships#displaying-data-from-relationships TextColumn::make('author.name') Now let's assume that the model from the author relationship has a global scope on it. That global scope is going to ultimately prevent values from showing in the relationship column for the relevant non-scoped records. How do I apply withoutGlobalScope() or withoutGlobalScopes() to the relationship column's query? Thanks!
2 Replies
ChesterS
ChesterS5mo ago
You wouldn't apply it to the column, you would apply it to the resource or table query itself. So (depending on what you want) either do
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with(['author' => fn ($query) => $query->withoutGlobalScopes()]);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with(['author' => fn ($query) => $query->withoutGlobalScopes()]);
}
Something like that. Or you could remove the scope from the relationship itself. Anyway, depends on what you want to do eg
public function allAuthors(): BelongsTo
{
return $this->belongsTo(Author::class)
->withoutGlobalScope(SoftDeletingScope::class);
}
public function allAuthors(): BelongsTo
{
return $this->belongsTo(Author::class)
->withoutGlobalScope(SoftDeletingScope::class);
}
pjb
pjbOP5mo ago
Thanks @ChesterS ! Looks like that second method is going to work for my use-case since I'll need this across multiple resources.

Did you find this page helpful?