/**
  * @param $id
  * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  *
  * SHOW PRODUCTS BY CATEGORIES
  */
 public function showProductByCategory($id)
 {
     $products = Product::whereRaw("category_id = {$id} and status = true")->orderBy('published_at', 'desc')->with('category', 'tags', 'image')->paginate(10);
     if ($id == 1) {
         $cat_product = "sabres lasers";
     } elseif ($id == 2) {
         $cat_product = "casques";
     }
     return view('front.categories.index', compact('products', 'cat_product'));
 }
 /**
  * Products by Category
  * @param  [type] $id [description]
  * @return [type]     [description]
  */
 protected function getCategory_Product($id = 0)
 {
     if ($id == 0) {
         return Product::whereRaw('status_product <> 0')->get();
     } else {
         $category = Category::find($id);
         $product = Category::find($id)->product()->whereRaw('status_product <> 0')->get();
         return array('name' => $category->name, 'list' => $product);
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(Request $request)
 {
     $product = $request->get('product');
     $quantity = $request->get('quantity');
     $data = [];
     for ($i = 0; $i < count($product); $i++) {
         if (!$product[$i] || !$quantity[$i] || $quantity[$i] <= 0 || !is_integer((int) $quantity[$i])) {
             $errors[] = 'product and quantity is required and > 0';
         }
         $data[] = ['product_id' => $product[$i], 'quantity' => $quantity[$i]];
     }
     if (isset($errors) && count($errors) > 0) {
         return redirect()->back()->withErrors($errors)->with('products', $data);
     }
     $ids = implode(',', $product);
     $products = Product::whereRaw("id IN ({$ids})")->get();
     $counter = 0;
     foreach ($products as $product) {
         if ($product->quantity < $data[$counter]['quantity']) {
             $errors[] = $product->name . 'quantity must be reduced' . ' max(' . $product->quantity . ')';
         }
         $counter++;
     }
     if (isset($errors) && count($errors) > 0) {
         return redirect()->back()->withErrors($errors)->with('products', $data);
     }
     DB::transaction(function () use($data) {
         $newData = [];
         foreach ($data as $value) {
             $newData[$value['product_id']] = ['quantity' => $value['quantity']];
         }
         $order = Order::create(['status' => 1, 'user_id' => Auth::user()->id, 'address_id' => null]);
         $order->products()->attach($newData);
         $order->setQuantity($increase = false);
         event(new OrderWasPlaced($this->user, $order));
     });
     return redirect(route('orders.index'))->with('success', 'Order successful created');
 }
 /**
  * find a products by name
  * @return [type] [description]
  */
 protected function searchNameProduct($nameproduct)
 {
     return $a = Product::whereRaw("name like '%{$nameproduct}%' and status_product <> 0")->get()->toArray();
 }