Laravel Query Builder 没有 运行 正确查询,即使它已正确绑定
Laravel Query Builder does not run query correctly even thought it binds correctly
我的 Laravel 查询生成器有问题。当我尝试将包含某种 sql 代码的变量绑定到我的绑定参数时,returns 没有结果。如果我 运行 enableQueryLog() 我可以看到查询和绑定是正确的。所以代码提供了一个完美的查询,但它没有相应地执行。
我已经尝试打印出所有重要的变量。我启用了一个查询日志来查看是否所有设置都正确,确实如此。当我在没有绑定的情况下将变量放入我的 whereRaw() 时,它工作正常。只是没有绑定。
这是我的代码运行:
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));
}
}
我知道这种绑定功能的使用是不必要的,它只是对我想构建的其他一些功能的测试。
这是我的查询日志 returns:
array:1 [▼
0 => array:3 [▼
"query" => "select `id`, `name` from `ni_carbrands` where :name"
"bindings" => array:1 [▼
0 => "name != Ford"
]
"time" => 190.25
]
]
查询的组成部分似乎都是正确的,但生成它似乎有些问题。
预期的结果是这样的:
{
"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"
}
]
}
但实际结果是:
{"info":1,"status":"Successfully found car brands","details":[]}
非常感谢您的帮助。
糟糕的原始状态:
whereRaw(':name',['name'=>$name])
就像这样:
$brands = DB::table('ni_carbrands')
->select('id','name')
->whereRaw($name)
->get();
你这个也可以用这个
$brands = DB::table('ni_carbrands')
->select('id','name')
->where('name', '!=', 'Ford')
->get();
否则你可以在动态字段中设置where条件
喜欢
->where($name, '!=', 'Ford')
您似乎无法绑定包含运算符的字符串。
看看这个 Can I bind a parameter to a PDO statement as a comparison operator?
还有这个
$name = 'name != Ford';
$brands = DB::table('ni_carbrands')
->whereRaw(':name',['name'=>$name])
->select('id','name')
->get();
这是等效查询
select from ni_carbrands where 'name != Ford'
当然不行,因为你有
select from ni_carbrands where name != Ford
引号问题
我的 Laravel 查询生成器有问题。当我尝试将包含某种 sql 代码的变量绑定到我的绑定参数时,returns 没有结果。如果我 运行 enableQueryLog() 我可以看到查询和绑定是正确的。所以代码提供了一个完美的查询,但它没有相应地执行。
我已经尝试打印出所有重要的变量。我启用了一个查询日志来查看是否所有设置都正确,确实如此。当我在没有绑定的情况下将变量放入我的 whereRaw() 时,它工作正常。只是没有绑定。
这是我的代码运行:
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));
}
}
我知道这种绑定功能的使用是不必要的,它只是对我想构建的其他一些功能的测试。 这是我的查询日志 returns:
array:1 [▼
0 => array:3 [▼
"query" => "select `id`, `name` from `ni_carbrands` where :name"
"bindings" => array:1 [▼
0 => "name != Ford"
]
"time" => 190.25
]
]
查询的组成部分似乎都是正确的,但生成它似乎有些问题。
预期的结果是这样的:
{
"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"
}
]
}
但实际结果是:
{"info":1,"status":"Successfully found car brands","details":[]}
非常感谢您的帮助。
糟糕的原始状态:
whereRaw(':name',['name'=>$name])
就像这样:
$brands = DB::table('ni_carbrands')
->select('id','name')
->whereRaw($name)
->get();
你这个也可以用这个
$brands = DB::table('ni_carbrands')
->select('id','name')
->where('name', '!=', 'Ford')
->get();
否则你可以在动态字段中设置where条件 喜欢
->where($name, '!=', 'Ford')
您似乎无法绑定包含运算符的字符串。
看看这个 Can I bind a parameter to a PDO statement as a comparison operator?
还有这个
$name = 'name != Ford';
$brands = DB::table('ni_carbrands')
->whereRaw(':name',['name'=>$name])
->select('id','name')
->get();
这是等效查询
select from ni_carbrands where 'name != Ford'
当然不行,因为你有
select from ni_carbrands where name != Ford
引号问题