Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queueing collections with multiple model types is not supported. #28

Open
dhruva81 opened this issue Sep 27, 2021 · 6 comments
Open

Queueing collections with multiple model types is not supported. #28

dhruva81 opened this issue Sep 27, 2021 · 6 comments

Comments

@dhruva81
Copy link

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

@pascalbaljet
Copy link
Contributor

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.

@dhruva81
Copy link
Author

dhruva81 commented Oct 1, 2021

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

@pascalbaljet
Copy link
Contributor

Can you share the complete Livewire component?

@ccchapman
Copy link

ccchapman commented Oct 14, 2021

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.

@Tiagospem
Copy link

Tiagospem commented Nov 23, 2021

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

@berto309
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants