Ejemplo n.º 1
0
 /** 
  * observe product event deleting
  * 1. delete varian
  * 2. delete price
  * 3. delete category(and tag)
  * 4. delete label
  * 5. delete image
  * 6. act, accept or refuse
  * 
  * @param $model
  * @return bool
  */
 public function deleting($model)
 {
     $errors = new MessageBag();
     //1. delete product's varian
     $varians = Varian::where('product_id', $model->id)->get();
     foreach ($varians as $varian) {
         if (!$varian->delete()) {
             $errors->add('varian', $varian->getError());
         }
     }
     //2. delete product's price
     $prices = Price::where('product_id', $model->id)->get();
     foreach ($prices as $price) {
         if (!$price->delete()) {
             $errors->add('price', $price->getError());
         }
     }
     //3. delete product's categories
     $categories = CategoryProduct::where('product_id', $model->id)->get();
     foreach ($categories as $category) {
         if (!$category->delete()) {
             $errors->add('category', $category->getError());
         }
     }
     //4. delete product's label
     $labels = ProductLabel::where('product_id', $model->id)->get();
     foreach ($labels as $label) {
         if (!$label->delete()) {
             $errors->add('label', $label->getError());
         }
     }
     //5. delete product's image
     $images = Image::where('imageable_type', 'App\\Models\\Product')->where('imageable_id', $model->id)->get();
     foreach ($images as $image) {
         if (!$image->delete()) {
             $errors->add('image', $image->getError());
         }
     }
     if ($errors->count()) {
         $model['errors'] = $errors;
         return false;
     }
     return true;
 }