public function createCheckOut(Request $req)
 {
     //
     //
     $items = $req->all();
     //
     $failed = array();
     $data = $items['data'];
     for ($i = 0; $i < count($data); $i++) {
         $todo = CheckoutModel::create(['address' => $data[$i]['address'], 'user_id' => $data[$i]['user_id'], 'product_id' => $data[$i]['product_id'], 'productQty' => $data[$i]['productQty'], 'status' => 'pending']);
         $todo2 = ProductModel::find($data[$i]['product_id']);
         if ($todo2->stock_num - $data[$i]['productQty'] >= 0) {
             $todo2->stock_num = $todo2->stock_num - $data[$i]['productQty'];
             $todo2->save();
             $todo->save();
             $todos = ShoppingCartModel::where("user_id", "=", $data[$i]['user_id'])->where("product_id", "=", $data[$i]['product_id']);
             $todos->delete();
         } else {
             array_push($failed, $data[$i]);
         }
     }
     if (count($failed) == 0) {
         return response()->json(['message' => 'success', 'state' => 'check out'], 201);
     } else {
         return response()->json(['message' => 'out of stocks', 'state' => 'check out', 'data' => $failed], 400);
     }
     //
 }
 public function searchProduct($query)
 {
     $segments = explode('/', $query);
     $todos = ProductModel::where('product_name', 'like', '%' . $segments[0] . '%')->get();
     if (count($todos) > 0) {
         return ['message' => 'FOUND', 'state' => 'search results', 'result' => $todos];
     } else {
         return ['message' => 'NOT FOUND', 'state' => 'search results'];
     }
 }
 public function showPicture($id)
 {
     $segments = explode('/', $id);
     $todos = ProductModel::find($segments);
     //        echo(public_path().$todos->pluck('product_filepath')[0]);
     if (count($todos) > 0) {
         $path = $todos->pluck('product_filepath');
         $filename = $todos->pluck('product_filename');
         ob_end_clean();
         //          $todos['product_filename'];
         return response()->download(public_path() . $path[0], $filename[0], ['Content-Type' => 'image/png']);
     } else {
         return ['message' => 'NOT FOUND', 'state' => 'products picture'];
     }
 }