<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Madin Bloch</title>
    <description>The latest articles on DEV Community by Madin Bloch (@madin_bloch).</description>
    <link>https://dev.to/madin_bloch</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3255031%2F0a255409-16be-464c-986a-a14475639130.jpg</url>
      <title>DEV Community: Madin Bloch</title>
      <link>https://dev.to/madin_bloch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/madin_bloch"/>
    <language>en</language>
    <item>
      <title>Step-by-Step Guide to Building a Dynamic Search Filter in Laravel with jQuery &amp; AJAX</title>
      <dc:creator>Madin Bloch</dc:creator>
      <pubDate>Tue, 10 Jun 2025 07:07:36 +0000</pubDate>
      <link>https://dev.to/madin_bloch/step-by-step-guide-to-building-a-dynamic-search-filter-in-laravel-with-jquery-ajax-4h3c</link>
      <guid>https://dev.to/madin_bloch/step-by-step-guide-to-building-a-dynamic-search-filter-in-laravel-with-jquery-ajax-4h3c</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6u6b0xdvekwwao7zqrq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6u6b0xdvekwwao7zqrq.png" alt="Image description" width="800" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;While working on a recent project, I came across a requirement where users needed to filter a list of suppliers dynamically based on either the business name or the business type. Initially, all suppliers were shown in a dropdown, but once a filter and keyword were selected, the list had to update instantly without reloading the page.&lt;/p&gt;

&lt;p&gt;In this blog post, I’ll walk you through how I solved this by implementing a dynamic supplier search filter using Laravel, jQuery, and AJAX. Whether you’re new to Laravel or just want to improve your UI interactivity with AJAX, this guide is for you.&lt;/p&gt;

&lt;p&gt;You’ll learn how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a simple and intuitive search UI&lt;/li&gt;
&lt;li&gt;Make AJAX requests using jQuery&lt;/li&gt;
&lt;li&gt;Filter and return JSON responses from Laravel&lt;/li&gt;
&lt;li&gt;Dynamically update a dropdown based on user input&lt;/li&gt;
&lt;li&gt;Let’s jump right into it!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prerequisites&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Laravel 10 or 11 project&lt;/li&gt;
&lt;li&gt;jQuery included (via CDN or Laravel Mix/Vite)&lt;/li&gt;
&lt;li&gt;Familiarity with routes, controllers, and Blade views&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Create the Suppliers Table
&lt;/h2&gt;

&lt;p&gt;Let’s start by defining a basic suppliers table. Here’s the migration example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Schema::create('suppliers', function (Blueprint $table) {
    $table-&amp;gt;id('supplier_id');
    $table-&amp;gt;string('business_name');
    $table-&amp;gt;string('business_type');
    $table-&amp;gt;boolean('status')-&amp;gt;default(1);
    $table-&amp;gt;timestamps();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Seed your database with sample values like:&lt;/p&gt;

&lt;p&gt;Business Names: “PaperCorp Ltd”, “InkWell Inc”, “Stationery Hub”, “Printify”&lt;br&gt;
Business Types: “Printing”, “Packaging”, “Wholesale”, “Stationery”&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Create the Blade View with Filter UI
&lt;/h2&gt;

&lt;p&gt;Here’s how the filter and dropdown UI looks in Blade:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="d-flex align-items-center mb-3"&amp;gt;
    &amp;lt;div class="col-mg-3 mr-2"&amp;gt;
        &amp;lt;label for="filterCriteria" class="form-label mb-0"&amp;gt;Filter By:&amp;lt;/label&amp;gt;
        &amp;lt;select id="filterCriteria" class="form-control"&amp;gt;
            &amp;lt;option value="all"&amp;gt;Select filter&amp;lt;/option&amp;gt;
            &amp;lt;option value="business_type"&amp;gt;Business Type&amp;lt;/option&amp;gt;
            &amp;lt;option value="business_name"&amp;gt;Business Name&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="col-mg-2 mr-2"&amp;gt;
            &amp;lt;label for="search_suppliar_value" class="form-label mb-0"&amp;gt;Search:&amp;lt;/label&amp;gt;
            &amp;lt;input type="text" id="search_suppliar_value" class="form-control searchInput" placeholder=""&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;button class="btn btn-primary search_suppliar mt-4"&amp;gt;Search&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;select class="form-control selectpicker" id="paper_supplier" name="paper_supplier" data-live-search="true"&amp;gt;
        &amp;lt;option value=""&amp;gt;Choose supplier&amp;lt;/option&amp;gt;
    &amp;lt;/select&amp;gt;

 &amp;lt;div class="form-group row align-items-center"&amp;gt;
      &amp;lt;label class="col-lg-2 col-form-label text-right"&amp;gt;Supplier:&amp;lt;/label&amp;gt;
          &amp;lt;div class="col-lg-5 selectOption"&amp;gt;
               &amp;lt;input class="form-control input optionInput" type="text" style="display: none" /&amp;gt;
     &amp;lt;select class="form-control selectpicker selectedOption" id="paper_supplier" name="paper_supplier" data-live-search="true"&amp;gt;
          &amp;lt;option value=""&amp;gt;Choose supplier&amp;lt;/option&amp;gt;
   @if(isset($suppliers))
        @foreach($suppliers as $supplier)
        &amp;lt;option value="{{ $supplier-&amp;gt;supplier_id }}"&amp;gt;{{ $supplier-&amp;gt;business_type 
        @endforeach
   @elseif(isset($suppliersData) &amp;amp;&amp;amp; $suppliersData-&amp;gt;supplier_id != null)
         &amp;lt;option selected="selected" value="{{ $suppliersData-&amp;gt;supplier_id }}"&amp;gt;{{ $suppliersData-&amp;gt;business_name }}&amp;lt;/option&amp;gt;
        @foreach($allsuppliers as $supplier)
     &amp;lt;option value="{{ $supplier-&amp;gt;supplier_id }}"&amp;gt;{{ $supplier-&amp;gt;business_name }}&amp;lt;/option&amp;gt;
        @endforeach
   @else
    @foreach($allsuppliers as $supplier)
       &amp;lt;option value="{{ $supplier-&amp;gt;supplier_id }}"&amp;gt;{{ $supplier-&amp;gt;business_name }}&amp;lt;/option&amp;gt;
   @endforeach
   @endif
&amp;lt;/select&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: jQuery + AJAX Logic
&lt;/h2&gt;

&lt;p&gt;Now add the script to handle the dynamic search:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$('.search_suppliar').on('click', function (e) {
            e.preventDefault(); // Prevent the default form submission

            // Get filter criteria and search value
            var filterCriteria = $('#filterCriteria').val();
            var searchValue = $('#search_suppliar_value').val();

            // Perform an AJAX request to the server
            $.ajax({
                url: "{{route('search.supplier')}}", // Laravel route
                method: 'GET',
                data: {
                    filter: filterCriteria,
                    search: searchValue
                },
                    success: function (response) {
                    $('#paper_supplier').find('option').not(':first').remove(); // Clear old options

                    if (response.suppliers.length === 0) {
                        $('#paper_supplier').append('&amp;lt;option value=""&amp;gt;No suppliers found&amp;lt;/option&amp;gt;');
                    } else {
                        $.each(response.suppliers, function (key, supplier) {
                            let optionLabel = '';

                            if (filterCriteria === 'business_name') {
                                optionLabel = supplier.business_name;
                            } else if (filterCriteria === 'business_type') {
                                // You can either show type, or name + type
                                optionLabel = supplier.business_name + ' (' + supplier.business_type + ')';
                            } else {
                                optionLabel = supplier.business_name;
                            }

                            $('#paper_supplier').append('&amp;lt;option value="' + supplier.supplier_id + '"&amp;gt;' + optionLabel + '&amp;lt;/option&amp;gt;');
                        });
                    }

                    $('#paper_supplier').selectpicker('refresh');
                },

                error: function (xhr, status, error) {
                    console.    or('Error fetching data:', error);
                }
            });
        });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Define the Route
&lt;/h2&gt;

&lt;p&gt;In your routes\web.php:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('/search-supplier', [SupplierController::class, 'searchSupplier'])-&amp;gt;name('search.supplier');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Controller Logic
&lt;/h2&gt;

&lt;p&gt;In your Controller\SupplierController:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public function searchSupplier(Request $request)
{
    $filter = $request-&amp;gt;input('filter');
    $search = $request-&amp;gt;input('search');

    // Simple Logic

    $query = DB::table('suppliers')-&amp;gt;where('status', 1);
        if ($filter == 'business_type') {
            $query-&amp;gt;where('business_type', 'LIKE', '%' . $search . '%');
        } elseif ($filter == 'business_name') {
            $query-&amp;gt;where('business_name', 'LIKE', '%' . $search . '%');
        }
        $suppliers = $query-&amp;gt;orderBy('supplier_id', 'desc')-&amp;gt;get();
        return response()-&amp;gt;json(['suppliers' =&amp;gt; $suppliers]);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tips for Real-World Usage
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use Form Request validation if needed&lt;/li&gt;
&lt;li&gt;Structure controller logic using Repository and Service layers for maintainability&lt;/li&gt;
&lt;li&gt;Limit or paginate results to handle large datasets&lt;/li&gt;
&lt;li&gt;Apply middleware if route protection is needed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Output
&lt;/h2&gt;

&lt;p&gt;Your UI now allows users to select a filter criteria, type a search value, and see a dynamically updated list of suppliers without reloading the page. Super smooth and efficient!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This solution is ideal for enterprise applications such as inventory systems, vendor management, and custom CRMs. With Laravel’s backend power and jQuery’s frontend responsiveness, you can deliver a seamless UX.&lt;/p&gt;

&lt;p&gt;Now that you’ve seen how to build this from scratch, try extending it to more complex filtering, chained dropdowns, or even Vue or React-based dynamic interfaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding! 🚀&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>jquery</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
