Laravel 5 PHP - filter results with input value

只愿长相守 提交于 2019-12-06 04:07:30

问题


I have this where I get all my tournaments $tournaments = Tournament::all();. Now I want to let the user filter these on the go in the view... I would like to have an <input> where the user enters a few characters and then the results filter on tournaments that have those characters in the name.

I have found this* online but I dont know how to populate that $keyword. It would be best if the results filter after every character that is entered in the inputfield. If this is not possible, then make this a form that sends this $keyword to Controller and retrieves the new results on the same page!
* $tournament = Tournament::where('name', 'LIKE', '%'.$keyword.'%')->get();

How do I do this? I don't know... Please provide some code in the answer.

Thx


回答1:


Are you wondering how to get the input of a posted value? You can use the Input::get() method.

$keyword = Input::get('keyword');
if(isset($keyword)){
   $tournaments = Tournament::where('name', 'LIKE', "%$keyword%")->get();
}else{
   $tournaments = Tournament::all();
}

Also can also use jQuery UI Autocomplete function to progressively search for results.



来源:https://stackoverflow.com/questions/34799481/laravel-5-php-filter-results-with-input-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!