2014-11-16 103 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