Laravel Autocomplete search from Database MySQL PHP

Laravel Autocomplete search from Database MySQL PHP

In this Post We Will Explain About is Laravel Autocomplete search from Database MySQL PHP With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Laravel 5 dynamic autocomplete search using select2 JS AjaxExample

In this post we will show you Best way to implement Create an autocomplete text input in Laravel, hear for AutoComplete Textbox in Laravel with Dynamic Data using PHPwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

READ :  Angularjs Get data from array of objects in controller

Create a Products Table and Model

php artisan make:seeder UsersTableSeeder

Add Route and Controller using Laravel

First of all one thing Add these simple two routes in your web laravel application folder routes.php file.

app/Http/routes.php

    Route::get('autocomplete',array('as'=>'autocomplete','uses'=>'[email protected]'));
    Route::get('ajaxsearch',array('as'=>'ajaxsearch','uses'=>'[email protected]'));

Now first of you will create a simple CustomAutoComplController.php file in the following path app/Http/Controllershere and then

app/Http/Controllers/CustomAutoComplController.php

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use App\Item;
    class CustomAutoComplController extends Controller {
        
        public function index(){
            return view('autocomplete.index');
       }
        public function autoComplete(Request $request) {
            $sql_query = $request->get('term','');
            
            $Items=Item::where('name','LIKE','%'.$sql_query.'%')->get();
            
            $val_data=array();
            foreach ($Items as $Item) {
                    $val_data[]=array('value'=>$Item->name,'id'=>$Item->id);
            }
            if(count($val_data))
                 return $val_data;
            else
                return ['value'=>'Sorry : No Result Found!!','id'=>''];
        }
        
    }

Create Blade File

Now second one we will simple create index.blade.php file in here Path following resources/views/autocomplete/ folder or directory.

READ :  How to send WhatsApp Messages from PHP

    @extends('layouts.default')
     
    @section('content')
       <div class="row">
            <div class="col-xs-10 col-sm-10 col-md-10">
                <div class="form-group">
                    
                    {!! Form::text('search_query_string', null, array('placeholder' => 'Please Enter your Search Text','class' => 'form-control','id'=>'search_query_string')) !!}
                </div>
            </div>
        </div>
        
       <script>
       $(document).ready(function() {
        url = "{{ route('ajaxsearch') }}";
         $("#search_query_string").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: url,
                    dataType: "json",
                    data: {
                        term : request.term
                    },
                    success: function(resultdata) {
                        response(resultdata);
                       
                    }
                });
            },
            minLength: 3,
           
        });
    });
    </script>
    @endsection

Please Don’t forget to add your libs Js file and Css file in project apps your master/default layout.

your master/default layout

<link href="jquery.ui.autocomplete.css" rel="stylesheet">
<script src="jquery.js"></script>
<script src="jquery-ui.min.js"></script>

Example

I hope you have Got What is AutoComplete Textbox in Laravel with Dynamic Data using PHP And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

READ :  AngularJs Round to 2 decimal places

Add a Comment

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