1

I am trying to add wheres to my query depending on what's coming in from 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>';
        }
    }
2
  • What do you get when you execute this, right before return? $queries = DB::getQueryLog(); $last_query = end($queries); Commented Nov 16, 2014 at 10:37
  • What you're doing wrong, is definitely not being specific on what you're trying to achieve and what the actual output is, that you find faulty. Commented Nov 17, 2014 at 8:39

1 Answer 1

2

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. 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
                    )
    );
}
Sign up to request clarification or add additional context in comments.

8 Comments

That line is to bring back all th results that match the type only, regardless of other filters applied. That is for the count of products in a filter e.g. Ages 0 - 6 (5), so a user knows there will be 5 results if they apply that filter.
@imperium2335 I don't know if I understood you, but I've edited my answer
+1 Thanks it's nearly there. Just now the counts next to each filter only show counts for whats on that page. I.e. on the first page Ages 0 - 6 (3), but on page 2 Ages 0 - 6 (1), if you know what I mean.
@imperium2335 No, this count only do Product::where('type_id', $type_id)->count() so it doesn't care on what page you are, it only gets count of products with given $type_id, nothing more, so on every page this number will be exactly the same
@imperium2335 It doesn't clarify anything. You wanted to get count and you get it and get also paginated product with extra age_id criterium
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.