/**
  * Change the Category
  *
  * @param $newCategoryId
  * @param Category $category
  * @param Product $product
  */
 private function changeCategory($newCategoryId, Category $category, Product $product)
 {
     //delete old category
     $product->categories()->detach($category->id);
     //add new one
     $product->categories()->attach($newCategoryId);
 }
 /**
  * Create a new user instance after a valid registration.
  *
  * @param  array $data
  * @param Category $category
  * @return User
  */
 public function create(array $data, Category $category)
 {
     $thumbnail = $this->extractThumbnail($data['thumbnail'], $category);
     $product = Product::create(['name' => $data['name'], 'description' => $data['description'], 'rating' => $data['rating'] ?: null, 'price' => $data['price'], 'thumbnail' => $thumbnail]);
     $category->products()->save($product);
     return $product;
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     DB::table('products')->truncate();
     DB::table('product_category')->truncate();
     $faker = Faker\Factory::create();
     $faker->addProvider(new Faker\Provider\Lorem($faker));
     $faker->addProvider(new Faker\Provider\en_US\Person($faker));
     $faker->addProvider(new Faker\Provider\Base($faker));
     $productsCount = 50;
     $categoriesCount = 10;
     $thumbnail = '1.jpg';
     for ($i = 0; $i < $productsCount; $i++) {
         $product = Product::create(['name' => $faker->sentence(6), 'description' => $faker->paragraph(3), 'rating' => $faker->numberBetween(1, 5), 'price' => $faker->numberBetween(1, 500), 'thumbnail' => $thumbnail]);
         $categoryId = $faker->numberBetween(1, $categoriesCount);
         $product->categories()->attach($categoryId);
     }
 }
 /**
  * update a product
  *
  * @param  array $data
  * @param Category $category
  * @param Product $product
  * @param Picture $pictureModel
  * @return Product
  */
 public function update(array $data, Category $category, Product $product, Picture $pictureModel)
 {
     $updateArray = [];
     foreach ($data as $key => $value) {
         if ($value !== '') {
             switch ($key) {
                 case 'new_product_id':
                     //find the new product by its id
                     $newProduct = Product::findOrFail($data['new_product_id']);
                     //associate with the new product
                     $pictureModel->product()->associate($newProduct)->save();
                     break;
                 case 'picture':
                     $updateArray['filename'] = $this->extractPicture($value, $category, $product, $pictureModel);
                     break;
             }
         }
     }
     $pictureModel->update($updateArray);
     return $pictureModel;
 }
 /**
  * If it succeded
  *
  * @return \Illuminate\View\View
  */
 public function success()
 {
     $product = Product::findOrFail($this->request->input('productId'));
     //get the user
     $user = $this->auth->user();
     return view('orders.success', compact('product', 'user'));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param $categoryId
  * @param $productId
  * @return Response
  */
 public function update($categoryId, $productId)
 {
     $this->updaterar->validator($this->request);
     $category = Category::findOrFail($categoryId);
     $product = Product::findOrFail($productId);
     $this->updaterar->update($this->request->all(), $category, $product);
     return redirect()->route('category.products.index', $category->id);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param $categoryId
  * @param $productId
  * @param  int $id
  * @return Response
  */
 public function destroy($categoryId, $productId, $id)
 {
     $category = Category::findOrFail($categoryId);
     $product = Product::findOrFail($productId);
     $picture = Picture::findOrFail($id);
     $picture->delete();
     return redirect()->route('category.products.pictures.index', [$category->id, $product->id]);
 }