laravel-cross-eloquent-search icon indicating copy to clipboard operation
laravel-cross-eloquent-search copied to clipboard

Queueing collections with multiple model types is not supported.

Open dhruva81 opened this issue 3 years ago • 6 comments

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

dhruva81 avatar Sep 27 '21 11:09 dhruva81

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.

pascalbaljet avatar Oct 01 '21 07:10 pascalbaljet

Hi thank you for reply. I am using this inside livewire component.

dhruva81 avatar Oct 01 '21 09:10 dhruva81

Can you share the complete Livewire component?

pascalbaljet avatar Oct 01 '21 11:10 pascalbaljet

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.

ccchapman avatar Oct 14 '21 02:10 ccchapman

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));
}

Tiagospem avatar Nov 23 '21 01:11 Tiagospem

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

berto309 avatar May 31 '23 10:05 berto309