/** * @param Searchable $parent * @param $updated * @param $relation * @param $key * @param array $with */ protected function addInvertedListener(Searchable $parent, $updated, $relation, $key, array $with) { $dispatcher = $this->container->make('events'); $event = 'eloquent.saved: ' . $updated; $dispatcher->listen($event, function ($model) use($parent, $relation, $key, $with) { $result = $parent->with($with)->whereHas($relation, function ($query) use($model, $key, $with) { $query->where($model->getKeyName(), '=', $model->getKey()); }); $documents = $result->get(); foreach ($documents as $document) { $this->update($document); } }); }