将两个参数传递给嵌套的 where 结果 "and `` is null" 附加到查询
Passing two arguments to nested where results in "and `` is null" appended to query
if ($input['search']) {
$args = array($query, $input);
$query->where(call_user_func_array(function($query, $input) {
$query->where('tbl_products.name', 'LIKE', '%' . $input['search'] . '%')
->orwhere('sku', 'LIKE', '%' . $input['search'] . '%');
}, $args));
}
}
return $query;
以上是我的查询的一部分,我打算在其中创建一个类似于以下内容的嵌套 where 子句:
WHERE
m.name = 'name' AND
(p.name LIKE "% example %" or p.sku LIKE "% example %")
我已经使用 'call_user_func_array' 将多个参数传递给闭包(这是我可以将用户输入传递给 where 子句的唯一方法)。
不幸的是,我收到一个看起来有点像这样的查询异常:
Unknown column '' in 'where clause'
...name
LIKE %example% or sku
LIKE %example% and `` is null
and `` is null 已附加到末尾。我认为这与需要两个参数的原始 where 子句有关,但我正在努力解决它。任何帮助将不胜感激。
尝试这样的事情:
if ($input['search']) {
$query->where(function ($query) use ($input) {
$query->where('tbl_products.name', 'LIKE', '%' . $input['search'] . '%')
->orWhere('sku', 'LIKE', '%' . $input['search'] . '%');
});
}
您可以通过 use
.
将变量传递给闭包
您不需要调用 call_user_func_array
并且您做错了。对于嵌套的 where
子句,在你的情况下你可以使用这样的东西:
// Assumed that you have $query variable, so
$query
->where('m.name', 'name') // = is optionl
->where(function($query) use ($input) // use copies the variable inside the function scope
{
$query
->where('p.name', 'LIKE', '%' . $input['search'] . '%')
->orWhere('p.sku', 'LIKE', '%' . $input['search'] . '%');
});
这是构建查询的示例,但您可能需要进行调整,因为您没有提供足够的信息。
if ($input['search']) {
$args = array($query, $input);
$query->where(call_user_func_array(function($query, $input) {
$query->where('tbl_products.name', 'LIKE', '%' . $input['search'] . '%')
->orwhere('sku', 'LIKE', '%' . $input['search'] . '%');
}, $args));
}
}
return $query;
以上是我的查询的一部分,我打算在其中创建一个类似于以下内容的嵌套 where 子句:
WHERE
m.name = 'name' AND
(p.name LIKE "% example %" or p.sku LIKE "% example %")
我已经使用 'call_user_func_array' 将多个参数传递给闭包(这是我可以将用户输入传递给 where 子句的唯一方法)。
不幸的是,我收到一个看起来有点像这样的查询异常:
Unknown column '' in 'where clause' ...
name
LIKE %example% orsku
LIKE %example% and `` is null
and `` is null 已附加到末尾。我认为这与需要两个参数的原始 where 子句有关,但我正在努力解决它。任何帮助将不胜感激。
尝试这样的事情:
if ($input['search']) {
$query->where(function ($query) use ($input) {
$query->where('tbl_products.name', 'LIKE', '%' . $input['search'] . '%')
->orWhere('sku', 'LIKE', '%' . $input['search'] . '%');
});
}
您可以通过 use
.
您不需要调用 call_user_func_array
并且您做错了。对于嵌套的 where
子句,在你的情况下你可以使用这样的东西:
// Assumed that you have $query variable, so
$query
->where('m.name', 'name') // = is optionl
->where(function($query) use ($input) // use copies the variable inside the function scope
{
$query
->where('p.name', 'LIKE', '%' . $input['search'] . '%')
->orWhere('p.sku', 'LIKE', '%' . $input['search'] . '%');
});
这是构建查询的示例,但您可能需要进行调整,因为您没有提供足够的信息。