© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
1 reply
Malja

Searchable select using model's attribute as label

Hi all,

I have a model
Pet
Pet
, that belongs to
User
User
:

class Pet extends Model {
  // ...
  public function owner(): BelongsTo {
    // This sets the field name in Pet, right? ↓
    return $this->belongsTo(User::class, 'owner_id');
  }
  // ...
}
class Pet extends Model {
  // ...
  public function owner(): BelongsTo {
    // This sets the field name in Pet, right? ↓
    return $this->belongsTo(User::class, 'owner_id');
  }
  // ...
}


class User extends Model {
  // ...
  public function pets(): HasMany {
    // This sets the field name in Pet, right? ↓
    return $this->hasMany(Pet::class, 'owner_id');
  }
  // ...
  public function fullName(): Attribute {
    return Attribute::make(
      get: fn () => "{$this->first_name} {$this->last_name}"
    );
  }
  // ...
}
class User extends Model {
  // ...
  public function pets(): HasMany {
    // This sets the field name in Pet, right? ↓
    return $this->hasMany(Pet::class, 'owner_id');
  }
  // ...
  public function fullName(): Attribute {
    return Attribute::make(
      get: fn () => "{$this->first_name} {$this->last_name}"
    );
  }
  // ...
}


However, I have a problem creating a new Pet for a specific User:

// from PetResource.php
public static function form(Form $form): Form {
  return $form->schema([
    Forms\Components\Select::make('owner_id')
      ->label('Owner')
      ->getSearchResultsUsing(fn ($search): array =>
          User::query()
            ->where('first_name', 'ilike', "%{$search}%")
            ->orWhere('last_name', 'ilike', "%{$search}%")
            ->get()
            ->toArray()
      )
      ->searchable()
      ->getOptionLabelUsing(fn ($value): string => User::query()->find($value->id)?->fullName)
      ->required()
  ]);
}
// from PetResource.php
public static function form(Form $form): Form {
  return $form->schema([
    Forms\Components\Select::make('owner_id')
      ->label('Owner')
      ->getSearchResultsUsing(fn ($search): array =>
          User::query()
            ->where('first_name', 'ilike', "%{$search}%")
            ->orWhere('last_name', 'ilike', "%{$search}%")
            ->get()
            ->toArray()
      )
      ->searchable()
      ->getOptionLabelUsing(fn ($value): string => User::query()->find($value->id)?->fullName)
      ->required()
  ]);
}


The problem is, that no matter what I do in
getSearchResultsUsing
getSearchResultsUsing
and
getOptionLabelUsing
getOptionLabelUsing
, I won't get User's
fullName
fullName
to be shown. I tried:

- Using
pluck('last_name', 'id')
pluck('last_name', 'id')
instead of
get
get
. This shows the
last_name
last_name
, but
getOptionLabelUsing
getOptionLabelUsing
is not called.
- Using
relationship('owner', 'last_name')
relationship('owner', 'last_name')
. This does not allow using
fullName
fullName
instead of
lastName
lastName
.

Do you have any suggestions?

Thank you.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

searchable attribute
FilamentFFilament / ❓┊help
2y ago
Custom attribute searchable
FilamentFFilament / ❓┊help
2y ago
Select searchable
FilamentFFilament / ❓┊help
2y ago
Making hidden attribute searchable
FilamentFFilament / ❓┊help
16mo ago