Any way to get record in searchable()

Hi all, I'm trying to get the record in searchable() for a polymorphic relationship where the searchable model property varies according to the model. I can get this working to return the correct property value in the formatStateUsing(), but can't find a way to get the record in searchable() Any ideas? Regards, Andy
TextColumn::make('customer')
->sortable()
->formatStateUsing(function ($record) {
$class = $record->customer_type;
$identifier = config('billing.class_identifier')[$class];
return $record->customer->$identifier;
})
->searchable(query: function (Builder $query, string $search): Builder {
// Similar to the formatStateUsing() function above

// Get the record's class
// $class = $record->customer_type;

// Look up the classname in config() which returns the searchable field
// eg customer.email or team.name
// $identifier = config('billing.class_identifier')[$class];


// Use that field in the query.
// return $query
// query->where($identifier, 'like', "%{$search}%")

return $query;
})
TextColumn::make('customer')
->sortable()
->formatStateUsing(function ($record) {
$class = $record->customer_type;
$identifier = config('billing.class_identifier')[$class];
return $record->customer->$identifier;
})
->searchable(query: function (Builder $query, string $search): Builder {
// Similar to the formatStateUsing() function above

// Get the record's class
// $class = $record->customer_type;

// Look up the classname in config() which returns the searchable field
// eg customer.email or team.name
// $identifier = config('billing.class_identifier')[$class];


// Use that field in the query.
// return $query
// query->where($identifier, 'like', "%{$search}%")

return $query;
})
2 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Dan Harrin
Dan Harrin2y ago
@_andypeacock searchable() is applied to the query. it does not have $record because it does not scope the query for 1 record, its just a query scope you probably want a whereHasMorph() (i dont know the exact name but it’s similar to that) where you can apply a different searchable scope based on the type of mprph
Want results from more Discord servers?
Add your server