我试图根据GET中的内容添加我的查询:
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 :(得分: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
)
);
}