Laravel where like not working properly

Related searches

I would like to use the query builder with the LIKE operator but it's not working properly.

Here is my code in my controller :

public function listall($query) {
        $Clubs = Club::where('', 'like', "%$query%")
                ->Join('leagues', 'clubs.league_id', '=', '')
                ->select('', '', 'clubs.blason', ' as league_name')

        return Response::json($Clubs);

Here is my 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

            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') }}' + '/' + + '" class="list-group-item"><span class="row">' +
                                '<span class="avatar">' +
                                    '<img src="{{asset('/')}}' + blason + '">' +
                                "</span>" +
                                '<span class="name">' + + '<br><small style="color:grey;">(Ligue ' + data.league_name + ')</small></span>' +

But its not working completely properly... In general, it finds results, but I'll give u an example of a search query. One possible query is "montagnarde". I'll give you the result for every letter. Typing:

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"

Does anybody see where is the problem? Thank you in advance!

I think your string concatenation is wrong.

Try to change where statement to

where('', 'LIKE', '%' . $query. '%')

Laravel where like not working. LIKE % % not working proper, You can try this, ->where('sale_property. property_ref_id', 'LIKE', DB::raw("'%$search%'")); Which will keep your dynamic generated string as it is without converting it through query builder or eloquent if you are using. Laravel where like not working properly . Posted by: admin November 27, 2017 Leave a comment. Questions: I would like to use the query builder with the LIKE operator

check now:

public function listall($query) {

dd( $query );

$clubs = Club::join('leagues', 'clubs.league_id', '=', '')
->where('', 'LIKE', '%' . $query . '%')
->select('', '', 'clubs.blason', ' as league_name')

dd( $clubs->toSql() );

return Response::json($clubs);


Like operator doesn't work, If I don't use Eloquent and just use the query builder, it works fine: $string = "Duck "; $results = Resellers::where('Contact_Name', 'like', '%'. If you are using the latest version for Laravel 5.X, you will see that sometime “php artisan serve” command doesn’t work properly. If you face this type of problem, you can use the below command and start your server locally.

@Dealeo You can write this. Hopefully this will solve your problem

public function listall($query) {
        $Clubs = Club::Join('leagues', 'clubs.league_id', '')
                ->where('', 'LIKE', '%' . $query . '%')
                ->select('', '', 'clubs.blason', ' as league_name')

        return Response::json($Clubs);

Like query not working, My like query is not working properly Here is it $food_search = Request::get(' search'); $result = Foods::where('food_name', 'like', '%'. The Laravel service container automatically injects these dependencies. If you would like to take total control over how the container injects dependencies into the handle method, you may use the container's bindMethod method. The bindMethod method accepts a callback which receives the job and the container.

You may have to use the search-query like this:

->where('', 'like', "%{$query}%")

Eloquent WHERE LIKE query escaping with '%' character behaves , Successfully merging a pull request may close this issue. None yet. 5 participants . @themsaid � @newms87 � @victorhugorch � @PandhuWibowo� The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything! Get Started

I suggest you to add COLLATE UTF8_GENERAL_CI to your table definition and then try your query like this (with leftJoin):

public function listall($query) {
        $Clubs = Club::leftJoin('leagues', 'clubs.league_id', '=', '')
                ->where('', 'like', "%$query%")
                ->select('', '', 'clubs.blason', ' as league_name')

        return Response::json($Clubs);

Collate utf8 suggestion from: How can I search (case-insensitive) in a column using LIKE wildcard?

eloquent count() does not work well with groupBy() � Issue #22883 , I don't know how this looks like from your perspective, but for me this is a huge problem If you would like to override this behavior, add the following lines to your Homestead.yaml file: provider: virtualbox natdnshostresolver: 'off' Symbolic Links On Windows. If symbolic links are not working properly on your Windows machine, you may need to add the following block to your Vagrantfile:

For Laravel to work properly, all requests must be routed to Laravel itself. This means we remove Nginx’s default 404 error handler and set it to /index.php?$query_string, which passes the request query to index.php file, a main Laravel application file. When you’ve made the above changes, you can save and close the file.

It is working properly if you take a deep dive into the method. What expectsJson() does is first check if the request is AJAX and !PJAX. It checks whether the request is AJAX by the existence of the X-Requested-With header and by checking if it's value is XMLHttpRequest. If that is true, that first part will pass.

Before serving Laravel with Apache, be sure to enable the mod_rewrite module so the .htaccess file will be honored by the server. If the .htaccess file that ships with Laravel does not work with your Apache installation, try this alternative: Options +FollowSymLinks -Indexes RewriteEngine On RewriteCond %{HTTP:Authorization} .

  • Instead of using ->get()at the end of your query, use ->toSql() and then print or dd() the result. This will show you what query is actually being run - useful debugging like you are doing now.
  • It looks like it's working correctly. What is the result you're expecting to see?
  • Here is the result : "select,, clubs.blason, as league_name from clubs inner join leagues on clubs.league_id = where like ? order by asc"
  • Thank you for your answer, but this is the same result with : where('', 'LIKE', '%' . $query. '%') or where('', 'LIKE', "%$query%")
  • There's nothing wrong with using "%$query%". PHP interpolates variables in double quoted strings.
  • Thank you for your answer, but I have the same results with your code.
  • @Dealeo-JeromeMansbendel I have updated my answer. Would you try this?