Level 44
This lesson would probably help you greatly when filtering your data, if you went this route you could then do ajax requests to a url with your filters in the query string:
is possible to make filter on this way with laravel and ajax: Ajax code:
function filter_data(){
$('.filter_data').html('<div id="loading"></div>');
var minimum_price = $('#hidden_minimum_price').val();
var maximum_price = $('#hidden_maximum_price').val();
var brand = get_filter('brand');
var size = get_filter('size');
var color = get_filter('color');
var cat = $('#cat').val();
var orderby = $('.orderby').val();
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: '/products',
type: 'get',
data: {minimum_price:minimum_price, maximum_price:maximum_price, brand:brand, size:size, orderby:orderby,color:color, cat:cat},
success:function(data){
$('.filter_data').html(data);
}
});
}
Controller
public function sviproizvodi(){
$products = DB::table('products as p')->join('product_attributes as pa','p.id','=','pa.product_id')->select('p.*','pa.size')->where('p.status', 1)->groupBy('p.id');
if (isset($request->minimum_price) && isset($request->maximum_price)) {
$products->whereBetween('p.price', [$request->minimum_price, $request->maximum_price]);
}
if (isset($request->brand)) {
$products->whereIn('p.brand_id', $request->brand);
}
if (isset($request->cat)) {
$products->whereIn('p.category_id', $request->cat);
}
if (isset($request->orderby)) {
if ($request->orderby == "standardno") {
$products->orderBy('p.id','desc');
}
if ($request->orderby == "istaknute") {
$products->orderBy('p.featured','desc');
}
if ($request->orderby == "novi") {
$products->orderBy('p.id','desc');
}
if ($request->orderby == "cijena1") {
$products->orderBy('p.price','asc');
}
if ($request->orderby == "cijena2") {
$products->orderBy('p.price','desc');
}
if (isset($request->size)) {
$products->whereIn('pa.size', $request->size);
}
if (isset($request->color)) {
$products->whereIn('pa.color', $request->color);
}
}
$proizvodi = $products->paginate(15);
return view('products',compact('proizvodi'));
}
Please or to participate in this conversation.