Laravel Pagination With Search Filter Not Working

  • 13-10-2022
  • 1269
  • Laravel 9
  • Haresh Chauhan

laravel pagination with the filter not working, After a lot's research, I have found some better solutions for fixing this issue in your laravel application. When you run your filter and then try to switch pagination that time your filter lost whatever your search in the filter.

If your filter not working with the pagination or you want the laravel pagination with a search filter this post will help you with five different examples.

So how to use search functionality with pagination in laravel, This post will help you with different examples. each example can teach you a lot about laravel pagination with the search filter.

Example 1. Compact With QueryString

You may use the withQueryString() method, if you would like to append all the current request URL query strings to the pagination link generated you can use like as in the below given example.

use App\Models\User;

public function index(Request $request)
{
    $users = User::query()
    ->select(
        'id',
        'name',
        'email',
        'mobile',
        'wallet'
    )
    ->when(isset($request->name),function($q) use ($request){
        $q->where('name','like',"%{$request->name}%");
    })
    ->paginate(20)
    ->withQueryString(); // ADD THIS

    return view('admin.user.user',compact('users'));
}

Use in blade file your pagination likes :

{{ $users->links() }}

Example 2. Link Pagination With QueryString

If you don't want to add the withQueryString() method to such a laravel query, you can directly use it in the blade file. There is no such difference even though you did in controller with the eloquent query.

use App\Models\User;

public function index(Request $request)
{
    $users = User::query()
    ->when(isset($request->name),function($q) use ($request){
        $q->where('name','like',"%{$request->name}%");
    })
    ->paginate(20);

    return view('admin.user.user',compact('users'));
}

Add the method in the blade file with the pagination:

{{ $users->withQueryString()->links() }}

Example 3. Add Path In URL

By Default, The pagination generates link generally matches the current request URL only, However, the withPath() method allows you to customization the URL path to the link generated from the pagination follow the example, and see the output.

use App\Models\User;

public function index(Request $request)
{
    $users = User::paginate(15);
    
    $users->withPath('/admin/users');
}
Output :

withPath() Method allows you to add a Custom URL path. See output something like this.

http://example.com/admin/users?page=1

Example 4. Appending Query String Values

If you want to add a query strig to the generated pagination, using the append() method you can add a query string to the generated pagination link following below given example.

use App\Models\User;

Route::get('/users', function () {
    
    $users = User::paginate(15);
    
    $users->appends(['sort' => 'votes']);
});
Output :

appends() method output URL.

http://example.com/admin/users?sort=votes

Example 5. Appending Hash Fragments

If you want to append "hash fragment" to the URL generated by the pagination, you may use the fragment() method. follow the below given example.

$users = User::paginate(15)->fragment('users');
Output :

fragment() method output used in the query. This will add #users fragment to all generated pagination URL.

http://example.com/admin#users

Laravel Pagination Not Working While Using Map Method [solved]


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :