0

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 ??

  • This code, generate 5 Arrays, but now with the right data ? $pst = Post::select('id','title','excerpt','body')->where('status', '=', static::PUBLISHED)->get(); return $pst->toArray(); Hmm, can anyone se my problem :-) – Henrik C Wiberg Oct 26 '17 at 21:33

2 Answers2

0

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

0

toSearchableArray is called for each entry. If you don't want to index the model with status set to DRAFT, you can return an empty array.

public function toSearchableArray()
{
    if ($this->status === 'DRAFT') {
        return [];
    }

    return $this->toArray();
}
Julien Bourdeau
  • 1,193
  • 11
  • 17