© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
12 replies
onursahindur

Select field hasMany relation

Hello all, I am trying to achieve a select field relation with a hasMany relation.

I have one Model called
Content
Content
and one model
ContentVisibility
ContentVisibility


ContentVisibility
ContentVisibility
holds a
content_id
content_id
, with a
type
type
field which can be either be BRANCH or COMMUNITY

in Content.php I have relations defined as;
public function visibilities(): HasMany {
    return $this->hasMany(ContentVisibility::class, 'content_id', 'id');
}
public function visibilities(): HasMany {
    return $this->hasMany(ContentVisibility::class, 'content_id', 'id');
}


also in ContentVisibility.php as;
public function content(): BelongsTo {
    return $this->belongsTo(Content::class, 'id', 'content_id');
}
public function content(): BelongsTo {
    return $this->belongsTo(Content::class, 'id', 'content_id');
}


I want to Make two select fields in "EditContent" resource which edits the relations.
I came up like these;
Select::make('branch_visibility_id')
    ->label('Show only to this branches...')
    ->multiple()
    ->options( ... )
    ->relationship('visibilities', fn (Builder $query) => $query->where('type', ContentVisibilityTypeEnum::BRANCH))
    ->searchable(),

Select::make('community_visibilities')
    ->label('Show only to this communities...')
    ->multiple()
    ->options(...)
    ->relationship('visibilities', fn (Builder $query) => $query->where('type', ContentVisibilityTypeEnum::COMMUNITY))
    ->searchable(),
Select::make('branch_visibility_id')
    ->label('Show only to this branches...')
    ->multiple()
    ->options( ... )
    ->relationship('visibilities', fn (Builder $query) => $query->where('type', ContentVisibilityTypeEnum::BRANCH))
    ->searchable(),

Select::make('community_visibilities')
    ->label('Show only to this communities...')
    ->multiple()
    ->options(...)
    ->relationship('visibilities', fn (Builder $query) => $query->where('type', ContentVisibilityTypeEnum::COMMUNITY))
    ->searchable(),


But as far as I understand "relationship" for select searches for a
BelongsToMany
BelongsToMany
relation, which I think it is not suitable for this situation.

Did I missed something?
Thanks.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Select HasMany relationship with CreateOption
FilamentFFilament / ❓┊help
2y ago
Select with hasMany?
FilamentFFilament / ❓┊help
3y ago
How to validate Select relation field?
FilamentFFilament / ❓┊help
16mo ago