I am creating an ecommerce system. I have a getCategories()
function in a controller. This function works but I tried to extend its functionality so that it would return the appropriate view according to a filter.
Lets say the user is viewing a specific category. Each category contains products and I tried to create a filter for the products based on their brand. For example if the user is in the category 'Guitars' he should be able to filter its products based on an existing brand 'Gibson'.
So I implemented the above scenario but I am not sure if that's very efficient. I've 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 am passing the parameter brand
through the url and this link calls the getCategories()
function, where inside that function I am checking if the link contains the parameter like so:
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 if
to return a different data to the view if the $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 am caching the categories route none of this would work because it would cache the view and it will refer to that cached file. Anything passed after the ?
gets ignored.
Route::get('categories/{slug}', array(
'as' => 'store.categories',
'uses' => 'StoreController@getCategories'
))->where('slug', '(.*)?')->before('cache.fetch')->after('cache.put');
How should I clean up/fix my code to get this functionality working and be able to cache the categories?