1024programmer Java Conditionally add index to LaravelScout (Algolia)

Conditionally add index to LaravelScout (Algolia)

I’m trying to add an index to Algolia using Laravel Scout based on a condition. For example, I have an Article model and I only want to add this article to Algolia if the article is active.My first method is:

public function toSearchableArray()
    if($this->active) return $record;
    return [];


This will only add active records, but will still try to add empty arrays, which are treated as operations in algolia (which I will charge for). The second way is to use shouldBesearchable() Scout functions:

public function shouldBeSearchable()
     if($this->active) return true;
     return false;


This doesn’t workphp artisan scout:import "App\Article". Has anyone encountered a similar problem?

1> Julien Bourd..:

This is a bug in Laravel Scout, shouldBeSearchable has not been released yet ( on the master branch), so you might run into issues like this.

Good news though: it’s just fixed by this PR.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/772394

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top