public function store(Request $request) { $input = $request->json()->all(); unset($input['categories']); $product = new Product($input); $product->save(); if (($categories = $request->json()->get('categories')) && !empty($categories)) { if (is_string($categories)) { $categories = explode(',', $categories); } $product->categories()->attach($categories); } $product->load('categories'); return response()->created($product); }
public function upsertProduct($productId, $userId, $data) { $product = $this->getProductByIdVendor($productId, $userId); $isAdd = false; if (!$product) { // New user product $product = new Product(); $product->vendor_id = $userId; //$product->uniquekey = (isset($row[8])) ? $row[8] : $row[11]; // MPC or GTIN $isAdd = true; } $product->name = ucfirst(mb_strtolower($data['name'])); $product->brand = isset($data['brand']) ? ucfirst(mb_strtolower($data['brand'])) : null; if (isset($data['pack'])) { $product->pack = $data['pack']; } if (isset($data['size'])) { $product->size = $data['size']; } if (isset($data['uom'])) { $product->uom = $data['uom']; } if (isset($data['serving_size_uom'])) { $product->serving_size_uom = $data['serving_size_uom']; } if (isset($data['mpc'])) { $product->mpc = $data['mpc']; } if (isset($data['broker_contact'])) { $product->broker_contact = $data['broker_contact']; } if (isset($data['gtin'])) { $product->gtin = $data['gtin']; } $product->is_halal = isset($data['is_halal']) ? 1 : 0; $product->is_organic = isset($data['is_organic']) ? 1 : 0; $product->is_kosher = isset($data['is_kosher']) ? 1 : 0; if (isset($data['calc_size'])) { $product->calc_size = $data['calc_size']; } if (isset($data['calculation_size_uom'])) { $product->calculation_size_uom = $data['calculation_size_uom']; } if (isset($data['calories'])) { $product->calories = $data['calories']; } if (isset($data['calories_from_fat'])) { $product->calories_from_fat = $data['calories_from_fat']; } if (isset($data['protein'])) { $product->protein = $data['protein']; } if (isset($data['carbs'])) { $product->carbs = $data['carbs']; } if (isset($data['fibre'])) { $product->fibre = $data['fibre']; } if (isset($data['sugar'])) { $product->sugar = $data['sugar']; } if (isset($data['total_fat'])) { $product->total_fat = $data['total_fat']; } if (isset($data['saturated_fats'])) { $product->saturated_fats = $data['saturated_fats']; } if (isset($data['sodium'])) { $product->sodium = $data['sodium']; } if (isset($data['product_image'])) { $product->product_image = $data['product_image']; } if (isset($data['description'])) { $product->description = $data['description']; } if (isset($data['preparation'])) { $product->preparation = $data['preparation']; } if (isset($data['ingredient_deck'])) { $product->ingredient_deck = $data['ingredient_deck']; } if (isset($data['features_benefits'])) { $product->features_benefits = $data['features_benefits']; } if (isset($data['allergen_disclaimer'])) { $product->allergen_disclaimer = $data['allergen_disclaimer']; } if (isset($data['net_weight'])) { $product->net_weight = $data['net_weight']; } if (isset($data['gross_weight'])) { $product->gross_weight = $data['gross_weight']; } if (isset($data['tare_weight'])) { $product->tare_weight = $data['tare_weight']; } if (isset($data['serving_size'])) { $product->serving_size = $data['serving_size']; } if (isset($data['vendor_logo'])) { $product->vendor_logo = $data['vendor_logo']; } if (isset($data['pos_pdf'])) { $product->pos_pdf = $data['pos_pdf']; } //$product->published = (isset($data['published']) ? 1 : 0); $product->published = $data['published']; // Sync allergens if (isset($data['allergens'])) { $product->allergens()->sync($data['allergens']); } // Sync categories if (isset($data['categories'])) { $product->categories()->sync($data['categories']); } $product->save(); return $product; }