jQuery AutoComplete Search with Laravel Part-2

Using jQuery AutoComplete Plugin via Laravel Route Function Part-2

Well in this part we will cover the same thing but via Controller. Do have a look at Part-1 first, then continue on to this one.

Note: Before starting, do download and include stylesheet and js file of jQuery UI OR load it directly from CDN.

Way #2: Controller Function

We will move directly to our Route as our Input field and Script will remain the same as Part-1.

So our Route will be:

Route::get('getfriendname', 'NameController@getfriendname');

So this route will go to “getfriendname” method of “NameController” so our controller will be:

public function getfriendname(Request $request)
 {

$term = $request->input('term');

$results = User::distinct()->select('given_name')->where('given_name', 'LIKE', '%'.$term.'%')->get();

//OR

// $results = DB::table("user")::distinct()->select('given_name')->where('given_name', 'LIKE', '%'.$term.'%')->get(); *

foreach ($results as $r)
 {
 $return_array[] = array('value' => $r->given_name);
 }

return $return_array;

//OR

// return Response::json($return_array); *

}

This “getfriendname” method is almost the same as the Route Function of Part-1.

Here I’ve provided two different ways to fetching the data from the database and two different ways to return it.

*Note: Don’t forget to use DB, Response and [table name] facade at the top.

And that’s all folks! Comment, Like & Share. Thanks! 🙂

Leave a Reply

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