Custom Searchable Query

I am having trouble getting a custom search query to work properly.
->searchable(
// query: fn(Builder $query, string $search) => $query->where('payment_codes.description', 'like', "%{$search}%")
query: function(Builder $query, string $search, RelationManager $livewire) {
$class = new TeamProfileItemsRelationManager;
// Log::debug($class->getDescriptionSearchQuery($search, $livewire->ownerRecord));
return $class->getDescriptionSearchQuery($search, $livewire->ownerRecord);
}
)
->searchable(
// query: fn(Builder $query, string $search) => $query->where('payment_codes.description', 'like', "%{$search}%")
query: function(Builder $query, string $search, RelationManager $livewire) {
$class = new TeamProfileItemsRelationManager;
// Log::debug($class->getDescriptionSearchQuery($search, $livewire->ownerRecord));
return $class->getDescriptionSearchQuery($search, $livewire->ownerRecord);
}
)
1 Reply
bwurtz999
bwurtz999OP15mo ago
My function returning the query is
public function getDescriptionSearchQuery($search, $flight)
{
$paymentCodesWithAliases = PaymentCodeAlias::where('team_id', $flight->team_id)->pluck('payment_code_id')->toArray();

$standardData = TeamProfileSelectedItem::where('flight_id', $flight->id)
->where('payment_codes.description', 'like', "%$search%")
// same as base query
);

$aliasData = TeamProfileSelectedItem::where('flight_id', $flight->id)
->where('payment_code_aliases.description', 'like', "%$search%")
// same as base query
);

return $standardData->union($aliasData);
}
public function getDescriptionSearchQuery($search, $flight)
{
$paymentCodesWithAliases = PaymentCodeAlias::where('team_id', $flight->team_id)->pluck('payment_code_id')->toArray();

$standardData = TeamProfileSelectedItem::where('flight_id', $flight->id)
->where('payment_codes.description', 'like', "%$search%")
// same as base query
);

$aliasData = TeamProfileSelectedItem::where('flight_id', $flight->id)
->where('payment_code_aliases.description', 'like', "%$search%")
// same as base query
);

return $standardData->union($aliasData);
}
When I apply get() and the Log the output, the data is properly filtered no matter what I put into the search field, it always returns all of the data In case anyone comes across this later, I wasn't able to solve this directly I ended up changing the structure of the models so I could use searchable with a custom relationship callback
->searchable(true, function (Builder $query, string $search) {
$query->whereHas(
relation: 'paymentCode',
callback: fn(Builder $q) => $q->where('description', 'like', "%{$search}%")
);
$query->orWhereHas(
relation: 'alias',
callback: fn(Builder $q) => $q->where('description', 'like', "%{$search}%")
);
}),
->searchable(true, function (Builder $query, string $search) {
$query->whereHas(
relation: 'paymentCode',
callback: fn(Builder $q) => $q->where('description', 'like', "%{$search}%")
);
$query->orWhereHas(
relation: 'alias',
callback: fn(Builder $q) => $q->where('description', 'like', "%{$search}%")
);
}),
Want results from more Discord servers?
Add your server