1 Answer 8588 Views
Writing a script to autocomplete search field is quite easy in php. But, when you are working with laravel, you'll need a method defined in a controller along with a route for the same method. Assuming that, you have already got your database populated, we're going to move on from that point.
The basic HTML element you'll need in order to build a search autocomplete functionality would be a search form. Since, we are autocomplete it without the search query string being sent to the URL, HTML <form> element won't be required here. So, lets add the search box in one of our blade file.
<input id="search" type="text" placeholder="Enter Keywords" />
Provide an id or a class to the input field so that we could call it in our jquery script. Here, we opted for an id named search.
In order to be able to work on the search autocomplete functionality with much convenience, we can include the jquery-ui.js and jquery-ui.css files from cdn.
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
Lets create a method in our controller that deals with displaying data in our frontend assuming the table we are working on is named posts.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class frontController extends Controller { // public function __construct(){} Public function livesearch(Request $request){ $term = $request->get('term'); $data = DB::table('posts')->where("title","LIKE","%$term%")->get(); foreach ($data as $result) { $results[] = ['value' => $result->title, 'link' => 'posts/'.$result->slug]; } return response()->json($results); } }
You can use this alternative method to retrive the data from database and return in JSON format if you don't need to add any additional phrase prior to the slug in URL. Keep in mind, the autocomplete jquery script requires the value and link from each dataset in JSON format.
Public function livesearch(Request $request){ $term = $request->get('term'); $data = DB::table('posts')->where("title","LIKE","%$term%")->select('title as value','slug as link')->get(); return response()->json($data); }
Since no method will be operational without a route in laravel, we need to create a route in our route.php file.
Route::get('livesearch','frontController@livesearch');
Finally, we can initialize the autocomplete jquery script that we added for this purpose in our blade template file where we want the autosearch functionality to be functional. Add this script anywhere below the link for jquery-ui.js file.
<script> $( "#search" ).autocomplete({ source: "livesearch", minLength: 1, select:function(event,ui) { location.href = ui.item.link; } }); </script>
You can include other options in this initialization script as well.Some of the most used options are as follows:
Option | Values | Description |
---|---|---|
autofocus | true / false | When set to true, the first item on the list will be focused by default. Default value is false. |
delay | integer in milliseconds | It will delay the display of the results by defined time. Default value is 300. |
position | any combination of left right top bottom and center | Defines the position of the suggestion list in relation to the input field. Default value is my: "left top", at: "left bottom", collision: "none" |
In this way, you can easily set up a search field with autocomplete functionality in laravel.
Leave a comment