Laravel Dynamic Autocomplete Search using Select2 JS Ajax - Part 1
https://www.itsolutionstuff.com/post/laravel-5-dynamic-autocomplete-search-using-select2-js-ajax-part-1example.html
Last updated
https://www.itsolutionstuff.com/post/laravel-5-dynamic-autocomplete-search-using-select2-js-ajax-part-1example.html
Last updated
By Hardik Savani January 19, 2017 Category : PHP Laravel Javascript Bootstrap jQuery MySql JSON AjaxPauseUnmuteLoaded: 2.45%FullscreenToday, I am going to share with you how to make dynamic autocomplete select dropdown from database using select2.js plugin in our laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 application.
Why we have to use autocomplete for our project ?, Sometimes we have more or thousands of records on our tables like users, products, tags etc, so that can not possible to handle in manage from select box. But you must require to make select dropdown for your products tables then you can do it using select2.js plugin. I already post autocomplete with Bootstrap Typeahead Plugin(Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo), But it is not possible with dropdown like key and value, This post will help to implement select box autocomplete value using select2 js ajax.
In this tutorial, i going to give you full example from scratch so you can easily understand and implement it on your project. You have to just follow few step to done this example. I also give you demo for testing in Part 2. So you can also see the demo on the next part. After finish this example you will see out put like as bellow screenshot:
Preview:
Step 1 : Install Laravel Application
we are going from scratch, So we require to get fresh Laravel 5.3 application using bellow command, So open your terminal OR command prompt and run bellow command:
Step 2 : Database Configuration
In this step, we require to make database configuration, you have to add following details on your .env file.
1.Database Username
2.Database Password
3.Database Name
In .env file also available host and port details, you can configure all details as in your system, So you can put like as bellow:
.env
Read Also: Multi select autocomplete jquery example with code
Step 3: Create categories Table
In this step we have to create migration for categories table using Laravel 5.3 php artisan command, so first fire bellow command:
After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create categories table.
Now we require to run migration be bellow command:
After create "categories" table, we should have some dummy data for testing, So make sure that.
Step 4: Create Route
In this is step we need to create route for form layout file and another one for ajax request. so open your routes/web.php file and add following route.
routes/web.php
Read Also: PHP - autocomplete websites with domain and logo using clearbit API
Ok Now we will see last two step in next page, so click on bellow button.