/** * @param Request $request * @param $course */ private function saveCourse(Request $request, $course) { $course->fill($request->input()); $productTags = $request->input('product_tags'); $productTagsIds = []; if ($productTags) { foreach ($productTags as $productTag) { $productTagDb = ProductTag::where('name', $productTag)->first(); if ($productTagDb == null) { $productTagDb = ProductTag::create(['name' => $productTag]); } array_push($productTagsIds, $productTagDb->id); } } $course->save(); $course->productTags()->sync($productTagsIds); $courseGalleryItemIds = []; if ($request->input('course-gallery-item-id') != null) { foreach ($request->input('course-gallery-item-id') as $key => $courseGalleryItemId) { $courseGalleryItem = CourseGalleryItem::findOrNew($courseGalleryItemId); $courseGalleryItemImageFile = $request->file('course-gallery-item-image')[$key]; if ($courseGalleryItemImageFile != null) { $courseGalleryItemImageFileName = str_random(32) . '.' . $courseGalleryItemImageFile->getClientOriginalExtension(); $courseGalleryItemImageFile->move(public_path() . '/img/uploads/', $courseGalleryItemImageFileName); $courseGalleryItem->image = 'img/uploads/' . $courseGalleryItemImageFileName; } $courseGalleryItem->course_id = $course->id; $courseGalleryItem->image_alt = $request->input('course-gallery-item-image-alt')[$key]; $courseGalleryItem->save(); if ($courseGalleryItemId != "") { array_push($courseGalleryItemIds, $courseGalleryItemId); } else { array_push($courseGalleryItemIds, $courseGalleryItem->id); } } } foreach ($course->courseGalleryItems as $courseGalleryItem) { if (!in_array($courseGalleryItem->id, $courseGalleryItemIds)) { $courseGalleryItem->delete(); } } $course->courseProgramItems()->delete(); if ($request->input('course-program-item-text') != null) { foreach ($request->input('course-program-item-text') as $courseProgramItemText) { $courseProgramItem = new CourseProgramItem(['text' => $courseProgramItemText]); $course->courseProgramItems()->save($courseProgramItem); } } }
/** * @param Request $request * @param $product */ private function saveProduct(Request $request, $product) { $product->fill($request->input()); $productColorIds = []; if ($request->hasFile('image')) { $fileName = str_random(32) . '.' . $request->file('image')->getClientOriginalExtension(); $request->file('image')->move(public_path() . '/img/uploads/', $fileName); $product->image = 'img/uploads/' . $fileName; } $productTags = $request->input('product_tags'); $productTagsIds = []; if ($productTags) { foreach ($productTags as $productTag) { $productTagDb = ProductTag::where('name', $productTag)->first(); if ($productTagDb == null) { $productTagDb = ProductTag::create(['name' => $productTag]); } array_push($productTagsIds, $productTagDb->id); } } $product->save(); $product->productTags()->sync($productTagsIds); if ($request->input('product-color-id') != null) { foreach ($request->input('product-color-id') as $key => $productColorId) { $productColor = ProductColor::findOrNew($productColorId); $productColorImage = $request->file('product-color-image')[$key]; if ($productColorImage != null) { $productColorImageFileName = str_random(32) . '.' . $productColorImage->getClientOriginalExtension(); $productColorImage->move(public_path() . '/img/uploads/', $productColorImageFileName); $productColor->image = 'img/uploads/' . $productColorImageFileName; } $productColorProductImage = $request->file('product-color-product-image')[$key]; if ($productColorProductImage != null) { $productColorProductImageFileName = str_random(32) . '.' . $productColorProductImage->getClientOriginalExtension(); $productColorProductImage->move(public_path() . '/img/uploads/', $productColorProductImageFileName); $productColor->product_image = 'img/uploads/' . $productColorProductImageFileName; } $productColor->product_id = $product->id; $productColor->code = $request->input('product-color-code')[$key]; $productColor->title = $request->input('product-color-title')[$key]; $productColor->shade = $request->input('product-color-shade')[$key]; $productColor->save(); if ($productColorId != "") { array_push($productColorIds, $productColorId); } else { array_push($productColorIds, $productColor->id); } } } foreach ($product->productColors as $productColor) { if (!in_array($productColor->id, $productColorIds)) { $productColor->delete(); } } $productGallery = $product->productGalleries()->first(); if ($productGallery == null) { $productGallery = new ProductGallery(); $productGallery = $product->productGalleries()->save($productGallery); } $productGallery->productGalleryItems()->delete(); if ($request->input('product_id') != null) { foreach ($request->input('product_id') as $recommendedProductId) { $productGallery->productGalleryItems()->save(new ProductGalleryItem(['product_id' => $recommendedProductId])); } } }