Filter bug in laravel using algolia

How to perform filter operation in laravel using algolia

Hi @aditkhadka7,

Welcome to the Algolia community.

By filter operation, do you mean a search where you filter on a specific attribute? If so, it depends whether you search from the front end using InstantSearch, or if you search from the backend with Laravel Scout or our API client.

Backend (Laravel Scout)

If you want to filter when searching in your models, you can filter by using where clauses on models that have the Searchable trait. For example, if you have a Product model, and you only want to search for products that are in stock, your code could look something like this:

$results = Product::search('your search term')->where('in_stock', true)->get();

Backend (API client)

If you want to search with filters using our PHP API client, you can pass the filters search parameter when you call the search method. For example, if you have a products index with all your products, and you only want to show the products that are in stock, your code would look something like this:

$client = Algolia\AlgoliaSearch\SearchClient::create('YourAppID', 'YourAPIKey');
$index = $client->initIndex('products');

$index->search('your search terms', [
  'filters' => 'in_stock:true'
]);

Frontend (InstantSearch)

If you’re using InstantSearch to search from the front end, you can filter search results by adding the filters search parameter in the configure widget. For example, if you have a products index with all your products, and you only want to show the products that are in stock, your code would look something like this:

// Initialize InstantSearch
const search = instantsearch({
  indexName: 'products',
  searchClient: algoliasearch('YourApplicationID', 'YourAPIKey'),
});

// Add the configure widget with your filters
search.addWidgets([
  instantsearch.widgets.configure({
    filters: 'in_stock:true',
  }),
  // Any other widgets
]);

// Start InstantSearch
search.start();

I hope this helps point you in the right direction, let me know if anything is still unclear.

Cheers!

Thank you devin beuwkees for you effort. I did exactly what you said but still data is not retrieving form algolia it returns null result. I am using algolia from laravel… Can you please tell me why data is not showing ?

Thanks for your reply.

Could you please post the PHP code you’re outputting in your screenshot? It seems like you’re dumping the query builder, the issue might be resolved if you chain the get method, like this:

App\Models\Teacher::search('math')->where('available_option', 'Home')->get();

Please let me know if this helps! :grinning_face_with_smiling_eyes:

This is Code:
$teachers = Teacher::search($string, [
‘facetFilters’ => [
“available_option:$method”
]
]);
dd($teachers);

when I retrive using get() it show another error thats why it is being more diffucult. This image shows error when I use get like you have explained

Could you please try to pass the filter by using custom search parameters instead by using the where method? You can do this in the following way:

$teachers = Teacher::search($string)
    ->with([
        'filters' => "available_option:$method"
    ])->get();

dd($teachers);

Also, please make sure that the available_option attribute is part of your attributesForFaceting.

You can read more about searching from your server in the Scout Extended documentation.

Let me know how it goes! :grinning_face_with_smiling_eyes: