model = $model; } /** * refresh. */ public function update($data) { $data['active'] = isset($data['active']) ? 1 : 0; if($data['id'] == "new"){ $this->model = Product::create($data); } else{ $this->model = $this->getById($data['id']); $this->model->fill($data); $this->model->save(); } $this->updateCategories(isset($data['categories']) ? $data['categories'] : array()); $this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : array()); return $this->model; } public function updateCategories($data = array()) { foreach ($this->model->categories as $category) { if(($pos = array_search($category->category_id, $data)) !== FALSE){ unset($data[$pos]); }else{ $category->delete(); } } //set attr if(is_array($data)){ foreach ($data as $id) { ProductCategory::create([ 'product_id' => $this->model->id, 'category_id' => $id, ]); } } return true; } public function updateAttributes($data = array()) { foreach ($this->model->attributes as $attribute) { if(($pos = array_search($attribute->attribute_id, $data)) !== FALSE){ unset($data[$pos]); }else{ $attribute->delete(); } } //set attr if(is_array($data)){ foreach ($data as $id) { ProductAttribute::create([ 'product_id' => $this->model->id, 'attribute_id' => $id, ]); } } return true; } public function delete() { } }