Ad

Laravel Query Builder Does Not Run Query Correctly Even Thought It Binds Correctly

- 1 answer

I have a problem with the Laravel Query Builder. When I try to bind a variable, which would include some sort of sql code, into my binding parameter, it returns no results. If I run enableQueryLog() I can see that the query and the binding is correct. So the code provides a perfectly fine query but yet it does not perform accordingly.

I've already tried printing out all the variables that matter. I enabled a query log to see if everything is set correctly, which it is. When I put in the variable in my whereRaw() just as it is without binding, it works fine. Just not with the binding.

This is the code I run:

public function getCarbrands() {

        $name = 'name != Ford';
        try {
            $brands = DB::table('ni_carbrands')
                ->whereRaw(':name',['name'=>$name])
                ->select('id','name')
                ->get();
            echo json_encode( array('info' => 1 ,'status' => 'Successfully found car brands', 'details' => $brands));

        } catch(Exception $e){

            echo json_encode( array('info' => 0 ,'status' => 'Error finding car brands', 'e' => $e)); 

        }

    }

I know that this use of the binding feature is unnecessary, it is merely a test for some other functions I wanna build. This is what my Query Log returns:

array:1 [▼
  0 => array:3 [▼
    "query" => "select `id`, `name` from `ni_carbrands` where :name"
    "bindings" => array:1 [▼
      0 => "name != Ford"
    ]
    "time" => 190.25
  ]
]

The components of the query all seem correct, but yet it seems to have some trouble producing it.

The expected results would be something like this:

{
    "info": 1,
    "status": "Successfully found car brands",
    "details": [
        {
            "id": 1,
            "name": "Toyota"
        },
        {
            "id": 2,
            "name": "Fiat"
        },
        {
            "id": 3,
            "name": "Iveco"
        },
        {
            "id": 4,
            "name": "Citroën"
        },
        {
            "id": 5,
            "name": "Opel"
        },
        {
            "id": 6,
            "name": "Mercedes"
        },
        {
            "id": 8,
            "name": "Volkswagen"
        },
        {
            "id": 9,
            "name": "Renault"
        },
        {
            "id": 10,
            "name": "MAN"
        },
        {
            "id": 11,
            "name": "Nissan"
        },
        {
            "id": 12,
            "name": "Hyundai"
        },
        {
            "id": 13,
            "name": "Peugeot"
        }
    ]
}

But the actual result is:

{"info":1,"status":"Successfully found car brands","details":[]}

I'd greatly appreciate some help.

Ad

Answer

It seems like you cant bind a string containing an operator.
Have a look into this one Can I bind a parameter to a PDO statement as a comparison operator? And this one binding not null in pdo

Ad
source: stackoverflow.com
Ad