Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Laravel

Searching and Pagination in Laravel

Add keyword search with query parameters and paginate Eloquent results in Blade.

Searching and Pagination in Laravel — a practical guide to Laravel pagination search with clear examples you can reuse in real projects.

Laravel Tutorial Series (19/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).

Short description

Search filters a query; pagination splits large result sets into pages with Laravel’s built-in paginator.

Controller

public function index(Request $request)
{
    $q = $request->string('q')->toString();

    $posts = Post::query()
        ->when($q !== '', function ($query) use ($q) {
            $query->where('title', 'like', "%{$q}%")
                  ->orWhere('body', 'like', "%{$q}%");
        })
        ->latest()
        ->paginate(10)
        ->withQueryString();

    return view('posts.index', compact('posts', 'q'));
}

Blade search form + links

<form method="GET" action="{{ route('posts.index') }}">
    <input type="search" name="q" value="{{ $q }}">
    <button type="submit">Search</button>
</form>

@foreach ($posts as $post)
    <h2>{{ $post->title }}</h2>
@endforeach

{{ $posts->links() }}

Leave a reply

Your email address will not be published. Required fields are marked *