Search code examples
phparrayslaravel-5algolialaravel-scout

toSearchableArray() send the same 1 row 7 times to Algolia?


This code retrieves one post from DB but sends it 7 times to Algolia ...

There are 7 items in the database

6 records with status = PUBLISHED

1 post with status = DRAFT

public function toSearchableArray()
{
    $array = Post::where('status', '=', static::PUBLISHED)->toArray();
      return $array;
}

  What am I doing wrong ??


Solution

  • Then I found the answer to my big question. enum field "PUBLISHED" turned to true, anything else false, so Algolia could now easily figure it out :-)

    This is the answer:

    $properties = $this->toArray();
    $properties['visible'] = $this->status === 'PUBLISHED';

    return $properties;

    :-) and goodnight