简体   繁体   English

Laravel查询不起作用

[英]Laravel query doesn't work

I am trying to add wheres to my query depending on what's coming in from GET: 我正在尝试根据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
                    )
    );
}

But all it's doing is bringing back every record. 但是,它所做的只是恢复所有记录。

What am I doing wrong? 我究竟做错了什么?


This is how I'm rendering my filters: 这就是渲染过滤器的方式:

    $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>';
        }
    }

You cannot create queries on object, you should do it this way: 您不能在对象上创建查询,您应该这样进行:

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
                    )
    );
}

You should also consider if it makes any sense to get all products and also paginated products. 您还应该考虑获取所有产品以及分页产品是否有意义。 If you have many products in your database it will take long time to get all your products. 如果您的数据库中有很多产品,那么将花费很长时间才能获得所有产品。

I'm also not sure what exactly you want to get for $productsPaginated . 我也不确定$productsPaginated到底想要得到什么。 I think you will need here building new query: 我认为您将需要在此处建立新查询:

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

EDIT 编辑

As you want to get count of products with only one filter, you should use here: 如果您只想使用一个过滤器来获取产品数量,则应在此处使用:

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
                    )
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM