Create Filter query according to parameter using Eloquent I want to filter products currently I am using below method its working fine. But whenever I want to apply pagination its not work. I tried with LengthAwarePaginator also but there is lot of stuff for me. So Please help and provide better approch if avialable, I am using laravel 5.6
$perpage = "1";
$offset = ($page - 1) * $perpage;
$query='SELECT * FROM `products` WHERE 1=1';
if(!empty($request->query('product'))){
$query.=' && `name` like "%'.$request->query('product').'%"';
}
if(!empty($request->query('price_from'))){
$query.=' && `sale_price`>='.$request->query('price_from').' && `sale_price`<='.$request->query('price_to');
}
if(!empty($request->query('stock_from'))){
$query.=' && `stock_qty`>='.$request->query('stock_from').' && `stock_qty`<='.$request->query('stock_to');
}
if(!empty($request->query('stock_status'))){
$query.=' && `stock_status` ='.$request->query('stock_status');
}
if(!empty($request->query('product_status'))){
$query.=' && `status` ='.$request->query('product_status');
}
$query.=' ORDER BY `id` DESC LIMIT '.$offset.','.$perpage;
$products= DB::select($query);
$productslinks= new LengthAwarePaginator($products, count($products), 1);
$productslinks->withPath(url('/kaapdaadmin/search-products'));
$query=DB::table('products');
if(!empty($request->query('product'))){
$query=$query->where('name','like','%'.$request->query('product').'%');
}
if(!empty($request->query('price_from'))){
$query=$query->where('sale_price','>=',$request->query('price_from'));
$query=$query->where('sale_price','<=',$request->query('price_to'));
}
if(!empty($request->query('stock_from'))){
$query=$query->where('stock_qty','>=',$request->query('stock_from'));
$query=$query->where('stock_qty','<=',$request->query('stock_to'));
}
if(!empty($request->query('stock_status'))){
$query=$query->where('stock_status',$request->query('stock_status'));
}
if(!empty($request->query('product_status'))){
$query=$query->where('status',$request->query('product_status'));
}
$query=$query->orderBy('id')->paginate($perpage);
return view('search-products',compact(query));
in view for pagnation link
$query->links();
Please sign in or create an account to participate in this conversation.