Ich möchte den Abfrage-Generator mit dem LIKE-Operator verwenden, aber es funktioniert nicht richtig.Laravel wo wie nicht richtig funktioniert
Hier ist mein Code in meinem Controller:
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);
}
Hier ist mein Javascript-Code:
<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>
Aber es ist nicht ganz richtig funktioniert ... Im Allgemeinen ist es Ergebnisse findet, aber ich Ich gebe Ihnen ein Beispiel für eine Suchanfrage. Eine mögliche Abfrage ist "montagnarde". Ich gebe dir das Ergebnis für jeden Brief. Eingabe:
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"
Wer sieht wo das Problem ist? Vielen Dank im Voraus!
Statt mit '-> get()' an Am Ende der Abfrage verwenden Sie '-> toSql()' und dann drucken oder 'dd()' das Ergebnis. Dies zeigt Ihnen, welche Abfrage tatsächlich ausgeführt wird - nützliches Debugging, wie Sie es jetzt tun. – James
Es sieht aus wie es richtig funktioniert. Welches Ergebnis erwarten Sie? –
Hier ist das Ergebnis: "wählen Sie' clubs.'id', 'clubs'.name',' clubs.'blason', 'ligen'.name' als' liga_name' aus 'clubs' 'Ligen' in' Clubs''league_id' = 'Ligen''id' wo''Club''Name' wie? Ordnen nach'Club''Name' asc" –