Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PersonalHomePage's avatar

Laravel 7.x Search with Pagination

Hi, would anyone be able to walk me through doing a search with pagination in Laravel 7.x?

For example I created this function in my UsersController:

class UsersController extends Controller
{
    public function index()
    {
        $data = User::paginate(25);
        return view('users.results', compact('data'));
    }
}

My User model gets the search request with this method

    public function scopeSearch($query, $search)
    {
        return $query->where('name', 'LIKE', "%$search%");
    }

Goal: output all users from the database inside of a ul>li

0 likes
13 replies
frankielee's avatar

Perhaps you can try this :

//if you are not passing the parameter by using route
public function index(Request $request){
	$data = User::search($request->search)->paginate(25);
	return view('users.results', compact('data'));
}

//if you are using the optional parameter in the route
public function index($search = "" ){
	$data = User::search($search)->paginate(25);
	return view('users.results', compact('data'));
}
1 like
Snapey's avatar

as @frankielee says, but make sure you append the search term to the paginator so that page 2,3 etc get the same search term

jlrdw's avatar

To add, are you wanting pretty url, or is query string okay?

If you are wanting pretty url's spatie has: https://github.com/spatie/laravel-paginateroute

Though you could program all the logic yourself, I've seen a lot of folks use the package.

But just thought I'd make you aware of that package.

PersonalHomePage's avatar

how do i pass the user input through the routes? right now I have this in my views

<input type="search" name="q" id="q" class="search-box" value="" placeholder="Search...">

and in the web.php :

Route::get('users','UsersController@index');

I tried that first method and it gives me everything that's in the db which is great. How do I search only based on user input with the scope instead?

PersonalHomePage's avatar

Thanks, but I'm just starting out so I'd rather focus on getting the core concepts right first. Pretty URLs will have to wait in the mean time lol

PersonalHomePage's avatar

doesn't paginate give different results on each page for the same term?

jlrdw's avatar

Example of appending to query string (paginator)

    public function index()
    {
      //  Validate and use authorization as per your app.

        $page = Request::input('page', '1');
        $dogsearch = !empty(Request::input('psch')) ? Request::input('psch') : '';
        $aval = !empty(Request::input('aval')) ? Request::input('aval') : '';
        $dogsch = $dogsearch . "%";

        $query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);

        $params = array('psch' => $dogsearch, 'aval' => $aval);

    return view('dog.index', compact('dogs', 'params'));
   }

Then view (pagination part)

{{ $dogs->appends($params)->links() }} 

route:

Route::middleware(['auth'])->group(function () {
    Route::get('dog/index', 'DogController@index');
    Route::post('dog/index', 'DogController@index');
   
    // other routes

Note: initial search is a post, thereafter is a get request for pagination. I have a video showing this in use, let me know if you want to view it, it's just a simple search example.

Also Pretty URLs is a core concept, you can use route parameters or a query string.

Reference from documentation: https://laravel.com/docs/7.x/routing#route-parameters

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

You are passing the search term as a field 'q'

So in the request;

class UsersController extends Controller
{
    public function index(Request $request)
    {
        $users = User::search($request->q)->paginate(25);

        return view('users.results', compact('users'));
    }
}

Thats all there is to it really.

In the view, output the $users collection, then for the pagination links

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

make sure the search form is using GET method not post

10 likes
anburocky3's avatar

Do we have to create macros for User::search to work right?

PersonalHomePage's avatar

Thank you for your help everyone. You have given me some great examples here and I will be able to move forward with my project. I really appreciate you guys taking the time to help me out!

Please or to participate in this conversation.