laravel-cross-eloquent-search
laravel-cross-eloquent-search copied to clipboard
Queueing collections with multiple model types is not supported.
Hi
I am first time using this package. I am trying to get search results from multiple models.
$this->results = Search::new()
->add(User::where('type', 'student'), ['name', 'email'])
->add(Question::class, ['id', 'text'])
->add(Chapter::class, ['name'])
->get($this->searchTerm);
But I am receiving following error.
LogicException
Queueing collections with multiple model types is not supported.
Kindly let me know, where I am doing wrong. Thanks
Are you using this package in a queued job somehow? This error comes from the getQueueableClass method, which is only used when the framework serializes models.
Hi thank you for reply. I am using this inside livewire component.
Can you share the complete Livewire component?
You can workaround it by moving your query into a computed property. Then you may wish to pass the computed property into the view via the render method.
Edit: My workaround seems to not work >= Livewire 2.6.0.
Same issue using livewire,
LogicException Queueing collections with multiple model types is not supported.
public $search = '';
public $searchResults = [];
public function updatedSearch(){
$this->searchResults = Search::new()
->add(User::class, [
'email',
'id'
])
->add(Auction::class, [
'title',
'id'
])
->add(Item::class, [
'title',
'id',
])
->beginWithWildcard()
->get(trim($this->search));
}
What I did was convert map the collections to determine which model the item was from, add the type of model (maybe Product model) and convert it to an array. On the frontend, when looping over items, check if the item is equal to a Model and use the right properties for that item. There have been changes in the search syntax. Checkout the the package readme.
// in your livewire component
public $searchQuery = '';
public array $searchResults = [];
public function updatedSearchQuery()
{
$results = [];
if (strlen($this->searchQuery) > 0) {
$results = Search::addMany([
[Product::activeSearchableResults(), 'name'],
[Brand::activeList(), 'name'],
[Category::activeSubCategories(), 'name'],
])->search(trim($this->searchQuery));
$this->searchResults = $results->map(fn($item) => [
'id' => $item->id,
'name' => $item->name,
'slug' => $item->slug,
'is_active' => $item->is_active,
'type' => class_basename($item)
])->toArray();
}
}
@foreach($searchResults as $res)
@if($res['type'] === 'Product')
<p> {{ $res['name'] }} </p>
@endforeach