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 :  Timepicker Angularjs Example - Timepicker Example angular-time-picker

Create a Products Table and Model

[php]
php artisan make:seeder UsersTableSeeder
[/php]

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
[php]
Route::get(‘autocomplete’,array(‘as’=>’autocomplete’,’uses’=>’CustomAutoComplController@index’));
Route::get(‘ajaxsearch’,array(‘as’=>’ajaxsearch’,’uses’=>’CustomAutoComplController@autoComplete’));
[/php]

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

[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’=>”];
}

}
[/php]

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 :  PHP mysql with Connecting to Database Using AngularJS

[php]
@extends(‘layouts.default’)

@section(‘content’)

{!! Form::text(‘search_query_string’, null, array(‘placeholder’ => ‘Please Enter your Search Text’,’class’ => ‘form-control’,’id’=>’search_query_string’)) !!}

$(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,

});
});

@endsection
[/php]

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

your master/default layout
[php]

[/php]

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 :  Five Tools to Get Started With Virtual Team Building

Leave a Reply

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