I have created some search filters using a textbox and drop downs so when I click the search button i want to use any of these filters to query eloquent relationships.
The search works fine and I get the correct amount of results but the problem lies with the pagination. When I click on the next page button (or any other page), the result is result and I get everything again.
I know this because I'm not doing a request when the paging is clicked so my question is how do i perform so same query but using the paging?
Here is my controller function
public function index(Request $request)
{
if(!empty($request)) {
$products = Product::with(['supplier', 'carrier', 'name']);
if($request->name) {
$products = $products->whereHas('Name', function ($query) use ($request) {
$products = $query->where('Name', $request->name);
});
}
if($request->subtype) {
$products = $products->whereHas('SubType', function ($query) use ($request) {
$products = $query->where('SubTypeId', $request->subtype);
});
}
if($request->supplier) {
$products = $products->whereHas('Supplier', function ($query) use ($request) {
$products = $query->where('SupplierId', $request->supplier);
});
}
if($request->carrier) {
$products = $products->whereHas('Carrier', function ($query) use ($request) {
$products = $query->where('CarrierId', $request->carrier);
});
}
$products = $products->paginate(8);
}
else {
$products = Product::with(['supplier', 'carrier', 'name'])->paginate(8);
}
$suppliers = Supplier::all();
$carriers = Carrier::all();
$subtypes = SubType::all();
$options = array(
'products' => $products,
'suppliers' => $suppliers,
'subtypes' => $subtypes,
'carriers' => $carriers
);
return view('products.index')->with($options);
}
I was able to fix it by changing the controller function. I didn't the IF statement
$products = Product::with(['supplier', 'carrier', 'name']);
if($request->name) {
$products = $products->whereHas('Name', function ($query) use ($request) {
$query->where('Name', $request->name);
});
}
if($request->subtype) {
$products = $products->whereHas('SubType', function ($query) use ($request) {
$query->where('SubTypeId', $request->subtype);
});
}
if($request->supplier) {
$products = $products->whereHas('Supplier', function ($query) use ($request) {
$query->where('SupplierId', $request->supplier);
});
}
if($request->carrier) {
$products = $products->whereHas('Carrier', function ($query) use ($request) {
$query->where('CarrierId', $request->carrier);
});
}
$products = $products->paginate(8);
$suppliers = Supplier::all();
$carriers = Carrier::all();
$subtypes = SubType::all();
$options = array(
'products' => $products,
'suppliers' => $suppliers,
'subtypes' => $subtypes,
'carriers' => $carriers
);
return view('products.index')->with($options);
In my view where I put the pagination link, I replaced
{{ $products->links() }}
with
{{ $products->appends(\Request::except('_token'))->render() }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.