php - Get Pagination after filter on collection Laravel
Get the solution ↓↓↓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.
Answer
Solution:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php_network_getaddresses: getaddrinfo failed: temporary failure in name resolution
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.