public function viewUnapprovedProduct($id)
 {
     $user = Auth::user();
     if ($user != null && $user->hasRole('admin')) {
         $coll = Collection::find($id);
         $collection = CollectionProduct::where('collection_id', '=', $id)->get();
         //  dd($collection) ;
         $collection_list = [];
         $creator = $coll->created_by;
         $creator = User::find($creator);
         foreach ($collection as $pro) {
             $product_id = $pro->product_id;
             $product = Product::find($product_id);
             $product = Product::add_created_by_and_category($product);
             if ($product != null) {
                 $created_by = $product['created_by'];
                 $category = Category::find($product['category_id']);
                 $category = $category['category'];
                 $user = User::find($created_by);
                 array_add($collection, 'category', $category);
                 array_add($collection, 'created_by_name', $user['name']);
                 array_push($collection_list, $product);
             }
         }
         return view('admin.viewUnapprovedCollection')->with('products', $collection_list)->with('collection', $coll);
     } else {
         return view('admin.login');
     }
 }
 public function searchBySlug($slug)
 {
     $coll = Collection::where('slug', '=', $slug)->first();
     $id = $coll->id;
     //  $products = Product::add_created_by_and_category($products);
     //  $coll = Collection::find($id);
     $collection = CollectionProduct::where('collection_id', '=', $id)->get();
     //  dd($collection) ;
     $collection_list = [];
     $creator = $coll->created_by;
     $creator = User::find($creator);
     foreach ($collection as $pro) {
         $product_id = $pro->product_id;
         $product = Product::find($product_id);
         $product = Product::add_created_by_and_category($product);
         if ($product != null) {
             $created_by = $product['created_by'];
             $category = Category::find($product['category_id']);
             $category = $category['category'];
             $user = User::find($created_by);
             array_add($collection, 'category', $category);
             array_add($collection, 'created_by_name', $user['name']);
             array_push($collection_list, $product);
         }
     }
     $collection = collect($collection_list)->sortByDesc('votes');
     return ['collection-info' => $coll, 'creator' => $creator, 'products' => $collection->values()->all()];
 }
 public function getByDate(Request $request)
 {
     $date = $request->input('date');
     $query = $request->input('query');
     if ($query == 'today') {
         $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '=', $date)->get();
     } else {
         if ($query == 'yesterday') {
             $yesterday = explode('-', $request->input('date'));
             $yesterday[2] = $yesterday[2] - 1;
             $yesterday = implode($yesterday, '-');
             $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '=', $yesterday)->get();
         } else {
             if ($query == 'this_month') {
                 $month = explode('-', $request->input('date'));
                 $month[2] = '0';
                 $month = implode($month, '-');
                 $start_date = explode('-', $request->input('date'));
                 if ($start_date[2] < 3) {
                     $start_date[2] = 0;
                     $start_date = implode($start_date, '-');
                 } else {
                     $start_date[2] = $start_date[2] - 2;
                     $start_date = implode($start_date, '-');
                 }
                 $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '>=', $month)->having('created_at_date', '<=', $start_date)->get();
                 // $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '>', $month)->get();
             } else {
                 if ($query == 'last_month') {
                     $start_date = explode('-', $request->input('date'));
                     $start_date[1] = $start_date[1] - 1;
                     if ($start_date[1] == 0) {
                         $start_date[0] = $start_date[0] - 1;
                         $start_date[1] = 12;
                     }
                     $start_date[2] = 0;
                     $end_date = $start_date;
                     $start_date = implode($start_date, '-');
                     // return ;
                     $end_date[2] = 31;
                     $end_date = implode($end_date, '-');
                     //      return $end_date;
                     $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '>=', $start_date)->having('created_at_date', '<=', $end_date)->get();
                 } else {
                     if ($query == 'this_year') {
                         $year = explode('-', $request->input('date'));
                         $year[1] = '0';
                         $year[2] = '0';
                         $year = implode($year, '-');
                         // return $year;
                         $start_date = explode('-', $request->input('date'));
                         if ($start_date[1] < 3) {
                             $start_date[1] = 0;
                             $start_date[2] = 0;
                             $start_date = implode($start_date, '-');
                             //   return $start_date;
                         } else {
                             $start_date[2] = 31;
                             $start_date[1] = $start_date[1] - 2;
                             $start_date = implode($start_date, '-');
                             //   return $start_date;
                         }
                         $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '>=', $year)->having('created_at_date', '<=', $start_date)->get();
                         //   $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '>', $year)->get();
                     } else {
                         if ($query == 'last_year') {
                             $start_year = explode('-', $request->input('date'));
                             $start_year[0] = $start_year[0] - 1;
                             $start_year[1] = '0';
                             $start_year[2] = '0';
                             $end_year = $start_year;
                             $start_year = implode($start_year, '-');
                             $end_year[0] = $end_year[0];
                             $end_year[1] = '12';
                             $end_year[2] = '31';
                             $end_year = implode($end_year, '-');
                             // return $year;
                             $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '>=', $start_year)->having('created_at_date', '<=', $end_year)->get();
                         } else {
                             if ($query == 'between') {
                                 $start_day = $request->input('start_day');
                                 $end_day = $request->input('end_day');
                                 $products = Product::orderBy('created_at_date', 'DESC')->orderBy('votes', 'DESC')->having('created_at_date', '>=', $start_day)->having('created_at_date', '<=', $end_day)->get();
                             }
                         }
                     }
                 }
             }
         }
     }
     // $products;
     //  return $products->sortByDesc('created_at');
     $products = Product::add_created_by_and_category($products);
     return response()->json(['products' => $products]);
     //  return json('products.index', ['products'=>$products]);
 }