Searchable Has Many Issue

Greetings, I was wondering how can i make column searchable by InventoryLocation name and also UserLocations.name but latest only. I have tried whereHas with subQuery but it does not work.. Any idea how to make it work?
public function table(Table $table): Table
{
return $table
->query(InventoryLocation::query()->with([
'userLocations' => function ($query) {
$query->latest()
->take(1);
},
]))
->columns([
TextColumn::make('name')
->getStateUsing(fn ($record) => $record->userLocations?->first()->data['name'] ?? $record->name)
->label('Location Name')
->searchable(query: function ($query, $search) {
$query->where('name', 'like', "%{$search}%");
})
->sortable(),
public function table(Table $table): Table
{
return $table
->query(InventoryLocation::query()->with([
'userLocations' => function ($query) {
$query->latest()
->take(1);
},
]))
->columns([
TextColumn::make('name')
->getStateUsing(fn ($record) => $record->userLocations?->first()->data['name'] ?? $record->name)
->label('Location Name')
->searchable(query: function ($query, $search) {
$query->where('name', 'like', "%{$search}%");
})
->sortable(),
3 Replies
Dennis Koch
Dennis Koch3w ago
Check the ->latestOfMany() relationship of Laravel. I think that would help
Dennis Koch
Dennis Koch3w ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
SuperUserDo
SuperUserDoOP3w ago
Thanks Dennis, will do try it out.

Did you find this page helpful?