/** * @param PropertyGroup $groups * @return string * @throws \Exception */ public function destroy(PropertyGroup $groups) { if ($groups->properties()->count() == 0) { if ($groups->delete()) { return json_encode(['status' => 'oke']); } } return json_encode(['status' => 'noke']); }
/** * Move from one group to another. * @param Request $request * @param PropertyGroup $groups * @param Property $properties */ public function moveProperty(Request $request, PropertyGroup $groups, Property $properties) { $from = $groups->find($request->get('from')); $to = $groups->find($request->get('to')); $property = $properties->find($request->get('property')); $position = $request->get('position'); //we move away from a group, meaning, everything that was behind this element //will now get a lower sort. Property::where('category_id', $property->category_id)->where('group_id', $from->id)->where('sort', '>', $property->sort)->update(['sort' => \DB::raw('sort - 1')]); //items that need to come behind our property get a higher sort Property::where('category_id', $property->category_id)->where('group_id', $to->id)->where('sort', '>=', $position)->update(['sort' => \DB::raw('sort + 1')]); //finally we save the property with it's new group and position $property->group_id = $to->id; $property->sort = $position; $property->save(); }
/** * @param Category $category * @return mixed */ protected function propertyGroups(Category $category) { return PropertyGroup::where('category_id', $category->id)->with('translations')->get(); }