我想将查询构建器与LIKE运算符一起使用,但它无法正常工作。
这是我控制器中的代码:
public function listall($query) {
$Clubs = Club::where('clubs.name', 'like', "%$query%")
->Join('leagues', 'clubs.league_id', '=', 'leagues.id')
->select('clubs.id', 'clubs.name', 'clubs.blason', 'leagues.name as league_name')
->orderBy('clubs.name')
->get();
return Response::json($Clubs);
}
这是我的Javascript代码:
<script type="text/javascript">
function hasard(min,max){
return min+Math.floor(Math.random()*(max-min+1));
}
jQuery(document).ready(function($) {
// Set the Options for "Bloodhound" suggestion engine
var engine = new Bloodhound({
remote: {
url: "{{ url('club/listall') }}"+'/%QUERY%',
wildcard: '%QUERY%'
},
datumTokenizer: Bloodhound.tokenizers.obj.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace
});
$(".club-search").typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
source: engine.ttAdapter(),
display: "name",
// This will be appended to "tt-dataset-" to form the class name of the suggestion menu.
name: 'clubsList',
// the key from the array we want to display (name,id,email,etc...)
templates: {
empty: [
'<div class="list-group search-results-dropdown"><div class="list-group-item">Aucun club trouvé.</div></div>'
],
header: [
'<div class="list-group search-results-dropdown">'
],
suggestion: function (data) {
if (data.blason == null) {
var aleat = hasard(1,4);
if (aleat == 1) {
var blason = "/images/blasons/blason-bleu.svg";
} else if (aleat == 2) {
var blason = "/images/blasons/blason-orange.svg";
} else if (aleat == 3) {
var blason = "/images/blasons/blason-rouge.svg";
} else if (aleat == 4) {
var blason = "/images/blasons/blason-vert.svg";
}
}
else {
var blason = "/images/blasons/" + data.blason;
}
return '<a href="{{ url('club') }}' + '/' + data.id + '" class="list-group-item"><span class="row">' +
'<span class="avatar">' +
'<img src="{{asset('/')}}' + blason + '">' +
"</span>" +
'<span class="name">' + data.name + '<br><small style="color:grey;">(Ligue ' + data.league_name + ')</small></span>' +
"</span>"
}
}
});
});
</script>
但是它没有完全正常工作......一般来说,它会找到结果,但我会给你一个搜索查询的例子。一个可能的问题是“montagnarde”。我会给你每封信的结果。打字:
m --> lot of results
mo --> lot of results
mon --> lot of results
mont --> lot of results
monta --> lot of results
montag --> lot of results
montagn --> lot of results
montagna --> no result
montagnar --> finds only "J.S. MONTAGNARDE"
montagnard --> finds only "J.S. MONTAGNARDE"
montagnarde --> finds only "J.S. MONTAGNARDE" and "LA MONTAGNARDE"
montagnarde i --> finds only "U.S. MONTAGNARDE INZINZAC"
有人看到问题在哪里吗? 提前谢谢!
答案 0 :(得分:0)
我认为你的字符串连接是错误的。
尝试将where语句更改为
where('clubs.name', 'LIKE', '%' . $query. '%')
答案 1 :(得分:0)
@Dealeo你可以写这个。希望这能解决您的问题
public function listall($query) {
$Clubs = Club::Join('leagues', 'clubs.league_id', 'leagues.id')
->where('clubs.name', 'LIKE', '%' . $query . '%')
->select('clubs.id', 'clubs.name', 'clubs.blason', 'leagues.name as league_name')
->orderBy('clubs.name')
->get();
return Response::json($Clubs);
}
答案 2 :(得分:0)
立即检查:
public function listall($ query){
dd( $query );
$clubs = Club::join('leagues', 'clubs.league_id', '=', 'leagues.id')
->where('clubs.name', 'LIKE', '%' . $query . '%')
->select('clubs.id', 'clubs.name', 'clubs.blason', 'leagues.name as league_name')
->orderBy('clubs.name');
dd( $clubs->toSql() );
return Response::json($clubs);
}
答案 3 :(得分:0)
您可能必须使用以下搜索查询:
->where('clubs.name', 'like', "%{$query}%")
答案 4 :(得分:0)
我建议您将COLLATE UTF8_GENERAL_CI
添加到您的表定义中,然后尝试这样的查询(使用leftJoin):
public function listall($query) {
$Clubs = Club::leftJoin('leagues', 'clubs.league_id', '=', 'leagues.id')
->where('clubs.name', 'like', "%$query%")
->select('clubs.id', 'clubs.name', 'clubs.blason', 'leagues.name as league_name')
->orderBy('clubs.name')
->get();
return Response::json($Clubs);
}
整理来自:How can I search (case-insensitive) in a column using LIKE wildcard?
的utf8建议答案 5 :(得分:0)
尝试一下
$Clubs = Club::where(DB::raw('LOWER(clubs.name)'), 'LIKE', '%'.strtolower($query).'%')
->Join('leagues', 'clubs.league_id', '=', 'leagues.id')
->select('clubs.id', 'clubs.name', 'clubs.blason', 'leagues.name as league_name')
->orderBy('clubs.name')
->get();
答案 6 :(得分:0)
MySQL 之类的语句和“%xyz 空格-bla-bla 123%”不起作用。 这不是 Laravel 的问题。