John Davidson

php - Laravel Scout/Meilisearch error when using filters/where clause

0 comments
Message:


When I try to use a where clause after the search method I get this:


Error


That's my Product model:
Product model


I've tried two methods to get the products of a specific store_id:


This one and this one (sorry for not using the code formatting I'm too dumb for that)


Any ideas about what I'm doing wrong?


I'm using PHP 8.0.10, Arch Linux, Meilisearch 0.22.0, Laravel 8.4, Laravel Scout 9.2 and meilisearch-php 0.19.1.


Thanks in advance.



My Answer:
It looks like the issue might be related to how you are using the `where` clause after the `search` method.

In Laravel Scout, the `search` method returns a collection of models that match the search query. If you want to further filter the results based on a specific condition, you should use the `where` method on the collection returned by the `search` method.

Here's how you can modify your code to achieve this:

php
$products = Product::search('query')->get();

$filteredProducts = $products->where('store_id', $store_id);

// Now you can use the $filteredProducts collection as needed


This way, you first retrieve all products that match the search query, and then filter the results based on the `store_id` using the `where` method on the collection.

Try this approach and see if it resolves the issue. Let me know if you need further assistance.

Rate this post

5 of 5 based on 7561 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us