Update index algolia

328 views Asked by At

I'm using Laravel 5.5 with Scout. I have an index in algolia using Documents and the users associated with theses Documents

class Documents extends Model
{
    use Searchable;

    public function toSearchableArray()
    {
      $data = $this->toArray();

      // formatting relationship for algolia

      $data['users'] = $this->types->toArray();
      $data['document_type'] = $this->typeDocuments->name;
      return $data;
    }

    protected $fillable = array('name', 'description', 'document_type');

    public function types() {
        return $this->belongsToMany('App\Users', 'document_rights', 'user_id', 'id');
    }

    public function typeDocuments() {
        return $this->belongsTo('App\Document_type', 'document_type');
    }
}

In my case, I will update one day the name of the Users, like this:

public function update(Request $request)
{
    $user = Users::find(5);
    $user->name = 'jean floriot';
    $user->update();
}

But it never changes the User in the index of Algolia. Any ideas how to proceed ?

1

There are 1 answers

2
Julien Bourdeau On BEST ANSWER

I believe you can use the searchable() method for that. After $this->update() you will get something like:

App\Documents::where('user_id', '=', $this->id)->searchable();

Please let me know if that worked or if I missed something.