How to Build a Search Feature in Laravel

A search feature is almost always needed — to search for articles, products, or users. In Laravel, building one is fairly simple with Eloquent. This article guides you through crea...

How to Build a Search Feature in Laravel
Advertisement

A search feature is almost always needed — to search for articles, products, or users. In Laravel, building one is fairly simple with Eloquent. This article guides you through creating a clean search, including across multiple columns at once.

Step 1: The Search Form

Create a simple form with the GET method so the keyword appears in the URL (great for sharing & SEO):

<form action="/articles" method="GET">
    <input type="text" name="q" value="{{ request('q') }}" placeholder="Search...">
    <button type="submit">Search</button>
</form>

Step 2: The Search Query in the Controller

public function index(Request $request)
{
    $query = Post::query();

    if ($request->filled('q')) {
        $keyword = $request->q;
        $query->where('title', 'like', "%{$keyword}%")
              ->orWhere('content', 'like', "%{$keyword}%");
    }

    $posts = $query->latest()->paginate(10);

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

Step 3: Searching Across Multiple Columns Safely

When combining the search with other filters, wrap the orWhere conditions in a group so the logic doesn't leak:

Advertisement
$query->where(function ($q) use ($keyword) {
    $q->where('title', 'like', "%{$keyword}%")
      ->orWhere('content', 'like', "%{$keyword}%");
});

Without the function wrapper, the orWhere conditions could cancel out your status/category filters.

Step 4: Preserve the Keyword During Pagination

So the keyword isn't lost when moving between pages, add the query string to the pagination links:

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

Step 5: Displaying the Results

@forelse ($posts as $post)
    <h3>{{ $post->title }}</h3>
@empty
    <p>No results for "{{ request('q') }}".</p>
@endforelse

Performance Tips

  • For large tables, LIKE %word% can be slow because it doesn't use an index. Consider a MySQL fulltext index for text search.
  • For large-scale search, use Laravel Scout with a driver such as Meilisearch or Algolia.

Conclusion

Building a search feature in Laravel just takes where('column', 'like', "%word%"), wrapped in a condition group when combined with other filters, and withQueryString() so the keyword persists during pagination. For very large data, upgrade to a fulltext index or Laravel Scout. To understand relationship queries, see our Eloquent Relationship guide.

Advertisement
fitur pencarian laravel search laravel cara membuat search laravel laravel where like pencarian data laravel
Share this article
Back to Blog
🚀 Partner Recommendation

Need Premium Source Code & Business Apps?

Access Laravel applications, POS systems, School Management, Clinic Software, ERP solutions, and ready-to-use premium source code at GudangCode.

GudangCode
  • ✔ Premium Source Code
  • ✔ Ready-to-Use Systems
  • ✔ Lifetime Updates
  • ✔ Lifetime Membership
  • ✔ Daily App Updates
Join Membership →
Advertisement
Latest Articles
Oct 14, 2026
Oct 13, 2026
Oct 11, 2026
Advertisement