Reputation: 914
I am using Laravel query builder to get data. Once I received data I put some filters to get relevant data, But after getting relevant data I lost default pagination. Any solution to come up against this problem ? Thanks in advance. My Code is
$tests = DB::table('view_tests')->whereIn('metric_id',$metricsIds)->paginate(4);
$tests = $tests->filter(function ($item) use ($var) {
return false !== stristr($item->name, $var) ||
false !== stristr($item->section_name, $var) ||
false !== stristr($item->method, $var) ||
false !== stristr($item->limit, $var) ||
false !== stristr($item->accredited_detail, $var);
return view('frontend.test_detailes',compact('tests'))->render();
And on View am using
{{ $tests->links() }}
Erro : Method Illuminate\Support\Collection::links does not exist.
Upvotes: 0
Views: 2108
Reputation: 2243
To increase performance, consider moving your filtering to the query builder instead of on the paginated results. This will also solve your problem regarding paginating. there are two options here:
Use the query builder (preferred)
The filters you have used in your example can be used on the query builder instead, as follows:
$tests = DB::table('view_tests')
->whereIn('metric_id',$metricsIds)
->where('name', '=', $var)
->paginate(4);
You can chain multiple constraints here. Please consult the Laravel documentation
Manually creating a paginator
Another option, which I would not advise, is to manually create a paginator. So change the ->paginate(4);
and create the paginator after filtering.
Upvotes: 1