public function index()
 {
     $categories = Input::get('cat');
     $per_page = 1000;
     //!IMPORTANT
     if ($categories != "") {
         if (Input::get("page") != "") {
             $page = (int) Input::get("page");
         } else {
             $page = 1;
         }
         if ($page == 1) {
             $page_calc = 0;
         } else {
             $page_calc = $page;
         }
         $cat_array = explode(",", $categories);
         $all_products = Product::join('product_cat', 'products.id', '=', 'product_cat.product_id')->whereIn('product_cat.cat_id', $cat_array)->get();
         $all_products_count = count($all_products);
         $last_page = ($all_products_count - $per_page) / $per_page + 1;
         $products_pivot = Product::join('product_cat', 'products.id', '=', 'product_cat.product_id')->whereIn('product_cat.cat_id', $cat_array)->skip($page_calc * $per_page - 1)->take($per_page)->get();
         $unique_products = [];
         foreach ($products_pivot as $item) {
             $found = false;
             foreach ($unique_products as $compare_item) {
                 if ($item->product_id == $compare_item->product_id) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 $unique_products[] = $item;
             }
         }
         $products = [];
         foreach ($unique_products as $product) {
             $new_product = Product::find($product->product_id);
             $new_product->product_photos = $new_product->product_photos()->where("is_preview", "=", 1)->get();
             $products[] = $new_product;
         }
         $products = $this->buildPaginationResponse($products, $all_products_count, $per_page, $page, $last_page, $categories);
     } else {
         //            $products = Product::paginate($per_page);
         $products = Product::with("product_photos")->with(array('product_photos' => function ($query) {
             $query->where('is_preview', '=', 1);
         }));
         $products = $products->paginate($per_page);
     }
     return $products;
 }
Exemple #2
0
 /**
  * @param $id
  * @return mixed
  */
 public function getProductsByTag($id)
 {
     $tag = new Tag();
     $listChildId = $tag->getIdChildsById($id);
     $listChildId[] = $id;
     $query = Product::join('product_tag', 'product.id', '=', 'product_tag.product_id')->whereIn('product_tag.tag_id', $listChildId)->orderBy('created')->get();
     return $query;
 }