public function detachAll(ElasticSearchIndexer $searchIndexer, $id) { $parent = $this->findParentEntity($id); $this->checkPermission(static::class . '@detachAll', ['model' => $parent]); $reindexItems = $searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]); $this->getRelation($parent)->detach(); // Reindex parent entity $searchIndexer->reindexOne($parent, []); // Reindex all detached entities $searchIndexer->reindexMany($reindexItems, []); return $this->getResponse()->noContent(); }
/** * Put many entities. * Internally make use of Relation::sync(). * * @param Request $request * @param string $id * @return ApiResponse */ public function putMany(Request $request, ElasticSearchIndexer $searchIndexer, $id) { $parent = $this->findParentEntity($id); $requestCollection = $request->json()->all(); $this->validateRequestCollection($requestCollection, $this->getChildModel()); $existingModels = $this->findChildrenCollection($requestCollection, $parent); $childModels = $this->fillModels($this->getChildModel(), $existingModels, $requestCollection); $this->checkPermission(static::class . '@putMany', ['model' => $parent, 'children' => $childModels]); // Collect all affected items $reindexItems = $searchIndexer->mergeUniqueCollection($searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]), $childModels); $relation = $this->getRelation($parent); if ($relation instanceof BelongsToMany) { $this->saveNewItemsInCollection($childModels); $relation->sync($this->makeSyncList($childModels, $requestCollection)); } else { $relation->saveMany($childModels); } // We need to reindex all affected items with relations $searchIndexer->reindexMany($reindexItems); // Reindex parent without relations $searchIndexer->reindexOne($parent, []); $this->postSync($parent, $childModels); return $this->getResponse()->transformer($this->getTransformer())->created()->collection($childModels); }