2014-11-16 104 views
1

我想這取決於什麼是從GET進來哪來添加到我的查詢:Laravel查詢不起作用

public function index($type_id) { 
    $Product = new Product; 
    $Product->where('type_id', $type_id); 
    if(array_key_exists('ages', Input::get())) { 
     $Product->where('age_id', $_GET['ages']); 
    } 
    $products = $Product->get(); 
    $productsPaginated = $Product->where('type_id', $type_id)->paginate(2); 
    return View::make('products.products', array(
       'products' => $products, 
       'productsList' => $productsPaginated 
        ) 
    ); 
} 

但是,所有它做的是帶回每個記錄。

我在做什麼錯?


這是我如何使我的過濾器:

$brands = $prices = $ages = $brandsUsed = $agesUsed = array(); 
    $out = ''; 
    foreach ($productsList as $product) { 
     $brands[$product->brands->id] = $product->brands->brand; 
     $brandsUsed[] = $product->brands->id; 
     $prices[] = $product->price; 
     $ages[$product->ages->id] = $product->ages->age; 
     $agesUsed[] = $product->ages->id; 
    } 

    $brandsUsed = array_count_values($brandsUsed); 
    $brands = array_unique($brands); 
    $params = Input::get(); 
    $lastParams = http_build_query($params); 
    unset($params['brand']); 
    $params = http_build_query($params); 
    if (count($brands) > 0) { 
     $out .= '<h5>Brands</h5>'; 
     foreach ($brands as $brandId => $brandName) { 

      if (stristr($lastParams, '&brand=' . $brandId) || stristr($lastParams, 'brand=' . $brandId)) { 
       $out .= '<a class="filter-link" href="' . Request::path() . '?' . $params . '">'; 
      } else { 
       $out .= '<a class="filter-link" href="' . Request::path() . '?' . $params . '&brand=' . $brandId . '">'; 
      } 
      $out .= '<span class="cbox">'; 

      if (stristr($lastParams, '&brand=' . $brandId) || stristr($lastParams, 'brand=' . $brandId)) { 
       $out .= '<span class="cbox-checked"></span>'; 
      } 

      $out .= '</span>'; 
      $out .= $brandName; 
      $out .= ' (' . $brandsUsed[$brandId] . ')'; 
      $out .= '</a>'; 
     } 
    } 
+0

怎麼辦當你在'return'之前執行時,你會得到什麼? '$ queries = DB :: getQueryLog(); $ last_query = end($ queries);' – Cyril

+0

你做錯了什麼,對於你想要達到的目標以及實際產出是多少,你發現有問題,絕對不是特定的。 –

回答

2

您不能創建查詢對象,你應該這樣來做:

public function index($type_id) { 
    $product = Product::where('type_id', $type_id); 
    if(array_key_exists('ages', Input::get())) { 
     $product->where('age_id', $_GET['ages']); 
    } 
    $productsAll = $product->get(); 
    $productsPaginated = $product->where('type_id', $type_id)->paginate(2); 
    return View::make('products.products', array(
       'products' => $productsAll, 
       'productsList' => $productsPaginated 
        ) 
    ); 
} 

你也應該考慮,如果獲得所有產品和分頁產品是有意義的。如果數據庫中有許多產品,則需要很長時間才能獲得所有產品。我也不確定你想得到什麼$productsPaginated。我想你會在這裏需要建立新的查詢:當你想要得到的產品數量只有一個過濾器

$productsPaginated = Product::where('type_id', $type_id)->paginate(2); 

編輯

,您應該使用這裏:

public function index($type_id) { 
    $product = Product::where('type_id', $type_id); 

    $productCount = $product->count(); 

    if(array_key_exists('ages', Input::get())) { 
     $product->where('age_id', $_GET['ages']); 
    } 
    $productsPaginated = $product->paginate(2); 

    return View::make('products.products', array(
       'productsCount' => $productCount, 
       'productsList' => $productsPaginated 
        ) 
    ); 
} 
+0

該行將返回僅與類型匹配的所有結果,而不管應用了其他過濾器。這就是過濾器中產品的數量,例如年齡0 - 6(5),所以用戶知道如果他們應用該過濾器將會有5個結果。 – imperium2335

+0

@ imperium2335我不知道我是否理解你,但我編輯了我的答案 –

+0

+1感謝它。就在現在,每個過濾器旁邊的計數器只顯示該頁面上的內容。即在第一頁0 - 6(3),但第二頁年齡0 - 6(1),如果你明白我的意思。 – imperium2335