imperium2335
imperium2335

Reputation: 24112

Laravel query doesn't work

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

Upvotes: 1

Views: 220

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

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

Upvotes: 2

Related Questions