示例#1
0
 /**
  * @param Request $request
  * @return string
  */
 public function removeCategory(Request $request)
 {
     $this->validate($request, ['product_id' => 'exists:products,id', 'category_id' => 'exists:product_categories,id']);
     $product = Product::find($request->get('product_id'));
     $category = Category::find($request->get('category_id'));
     $product->load('categories');
     $category->load('translations');
     if ($product->categories->contains($category->id)) {
         //is the category the main category or a synonym?
         if (!$category->original_id) {
             //we can only have 1 main category and synonyms to that category
             //so we can do an empty sync here
             $product->categories()->sync([]);
             return json_encode(['status' => 'flushed']);
         } else {
             $product->categories()->detach($category);
             return $category;
         }
     }
     return json_encode(['status' => false]);
 }