Laravel 4 - Filter Results and Cache

I am creating an e-commerce system. I have a function getCategories()

in my controller. This function works, but I tried to extend its functionality so that it returns the appropriate view according to the filter.

Let's assume the user is viewing a specific category. Each category contains products and I tried to create a filter for products based on their brand. For example, if a user is in the Guitars category, they should be able to filter their products based on the existing Gibson brand.

So, I've implemented the above scenario, but I'm not sure if it's very efficient. I created a link:

@foreach($brand as $key => $value)
  <a href="/store/categories/{{$category->slug}}?brand={{$value->slug}}">{{$value->name}}</a>
@foreach

      

As you can see, I pass a parameter brand

via url and this link calls a function getCategories()

, where inside this function I check if the link contains a parameter like this:

if (Input::has('brand')) {
    $brandflag = true;
    $brand_input = Input::get('brand');
    $brandfilter = array('type'=>'Brand', 'name' => ucfirst($brand_input));
}

      

$brandflag

is initially set to false

, and if it exists, it changes its value to true

. Also another one if

to return different data to the view if $brandflag

changed to true

.

if ($brandflag == true) {
    $b = Brand::whereSlug($brand_input)->first();
    $products = Product::where('brand_id','=', $b->id)->whereIn('category_id', $children->lists('id'));
    return View::make('store.categories')
        ->with('products', $products->paginate(6))
        ->with('ranges', $ranges)
        ->with('brandfilter', $brandfilter)
        ->with('category', $main)
        ->with('brands', $brands)
        ->with('children', $children_array)
        ->with('seo', $seo);
}

      

All of the above works, however when I cache the category route it won't work because it will cache the view and it will link to that cached file. Anything that passed after ?

is ignored.

Route::get('categories/{slug}', array(
    'as' => 'store.categories',
    'uses' => 'StoreController@getCategories'
))->where('slug', '(.*)?')->before('cache.fetch')->after('cache.put');

      

How do I clean / fix my code so this functionality works and categories can be cached?

+3


source to share


1 answer


I assume that the filters cache.put

and cache.fetch

based on this article .

In his example, the author uses $request->url()

to create a cache key. The API docs states that this method does not include a query string.



To cache work with query parameters, you need to replace the call with url()

by fullUrl()

or a combination of url()

and query()

.

+2


source







All Articles