示例#1
0
 /**
  * Creates / updates inventories
  * @param   array $inventories
  */
 private function saveInventories($inventories)
 {
     foreach ($inventories as $i => $inventory) {
         // Load the inventory model via Find, First, or New
         $inventoryModel = $inventory['id'] ? Inventory::find($inventory['id']) : Inventory::firstOrNew(['product_id' => $this->productId, 'name' => $inventory['name']]);
         // Something went wrong finding / creating the model
         if (!$inventoryModel) {
             return FALSE;
         }
         // Update model values
         $inventoryModel->product_id = $this->productId;
         $inventoryModel->name = $inventory['name'];
         $inventoryModel->quantity = $inventory['quantity'];
         $inventoryModel->modifier = $inventory['modifier'];
         $inventoryModel->is_active = $inventory['is_active'];
         $inventoryModel->position = $i;
         // Attempt to save the model
         if (!$inventoryModel->save()) {
             return FALSE;
         }
     }
 }
 /**
  * Delete a model
  *
  * @throws \October\Rain\Exception\AjaxException
  * @return mixed
  */
 public function onDeleteModel()
 {
     $id = input('id');
     $model = input('model');
     $sessionKey = input('sessionKey');
     // Delete an option
     if ($model === 'option') {
         if (Option::find($id)->delete()) {
             Flash::success(Lang::get('bedard.shop::lang.option.delete.success'));
             // Update the inventories partial
             $this->prepareVars($sessionKey);
             return ['[data-partial="inventories"]' => $this->makePartial('inventories')];
         }
         $error = Lang::get('bedard.shop::lang.option.delete.failed');
     } elseif ($model === 'inventory') {
         if (Inventory::find($id)->delete()) {
             return Flash::success(Lang::get('bedard.shop::lang.inventory.delete.success'));
         }
         $error = Lang::get('bedard.shop::lang.inventory.delete.failed');
     }
     throw new AjaxException($error);
 }