private function save($publicId = null) { if ($publicId) { $product = Product::scope($publicId)->firstOrFail(); } else { $product = Product::createNew(); } $product->product_key = trim(Input::get('product_key')); $product->notes = trim(Input::get('notes')); $product->pack_types = trim(Input::get('pack_types')); $product->ice = trim(Input::get('ice')); $product->units = trim(Input::get('units')); $product->cc = trim(Input::get('cc')); $product->save(); $data = json_decode(Input::get('data')); $pricesIds = []; $id = 1; foreach ($data->prices as $price) { if (isset($price->public_id) && $price->public_id) { $record = Price::scope($price->public_id)->firstOrFail(); } else { $record = Price::createNew(); } $record->price_type_id = $id; $record->cost = trim($price->cost); $product->prices()->save($record); $priceIds[] = $record->public_id; $id++; } foreach ($product->prices as $price) { if (!in_array($price->public_id, $priceIds)) { $price->delete(); } } if ($publicId) { Session::flash('message', trans('texts.updated_product')); } else { // Activity::createProduct($product); Session::flash('message', trans('texts.created_product')); } return Redirect::to('products/' . $product->public_id); }