这里我在我的laravel代码中使用typeahead.js autocomplete search jquery插件。你能帮我应用typeahead.js autocomplete search从laravel数据库的不同列中按lastname firstname或firstname lastname两种方式搜索用户名吗?
我的控制器逻辑是-
public function autocomplete(Request $request) {
$string = "%{$request->input('query')}%";
$searchValues = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
$users = Profile::where(function ($q) use ($searchValues) {
foreach ($searchValues as $value) {
$q->orWhere('firstname', 'like', "%{$value}%")->orWhere('lastname', 'like', "%{$value}%")->orWhere('mobile', 'like', "%{$value}%");
}
})->get(array('firstname','lastname','mobile'));
return response()->json($users);
}
1条答案
按热度按时间mrfwxfqh1#
试试这个