-
-
Save nasirkhan/cf36bf5bb4c31a917b0235b3c8543f1a to your computer and use it in GitHub Desktop.
Select2 and Laravel: Ajax Autocomplete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* For more details see: http://laraget.com/blog/select2-and-laravel-ajax-autocomplete */ | |
namespace App\Http\Controllers\Select2Ajax; | |
use App\Tag; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
class TagController extends Controller | |
{ | |
/** | |
* @param Request $request | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public function find(Request $request) | |
{ | |
$term = trim($request->q); | |
if (empty($term)) { | |
return \Response::json([]); | |
} | |
$tags = Tag::search($term)->limit(5)->get(); | |
$formatted_tags = []; | |
foreach ($tags as $tag) { | |
$formatted_tags[] = ['id' => $tag->id, 'text' => $tag->name]; | |
} | |
return \Response::json($formatted_tags); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* For more details see: http://laraget.com/blog/select2-and-laravel-ajax-autocomplete */ | |
Route::get('/tags', function() { | |
return view('tags'); | |
}); | |
Route::get('/tags/find', 'Select2Ajax\TagController@find'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment