/** * Update a category's inheritance * * @param \Illuminate\Database\Eloquent\Collection $categories * @return void */ public function updateInheritance(Collection $categories) { $branch = []; if ($this->parent_id !== null) { $parent = $categories->whereLoose('id', $this->parent_id)->first(); while ($parent && $parent->is_inheriting) { $branch[] = $parent->id; $parent = $categories->whereLoose('id', $parent->parent_id)->first(); } } $branch[] = $this->id; $this->ancestors()->sync($branch); }