这是我的模型:
$AVMatchCriteria = device::where([$AVMatch])->get();
我正在创建变量 $AVMatch,方法是使用 foreach 循环遍历我表中的条目,下面是我回显它时的样子。
["category", "=", "Server"],["siteid", "!=", "145228"],["udf8", "=", "asd"]
运行我的代码时出现以下错误:
但是,如果我只是以纯文本形式复制字符串并将其放入如下模型中,它就可以正常工作。我不确定为什么它不起作用,但它以某种方式在我的查询中放置了 0 =,我猜这就是问题所在。
$AVMatchCriteria = device::where([["category", "=", "Server"],["siteid", "!=", "145228"],["udf8", "=", "asd"]])->get();
任何建议都很好 :D
下面是我创建变量 $AVMatch 的方法,我知道它有点落后,但我正在尝试修复它:
$AVMatch = '';
$count = 0;
foreach($AVMatchCriteria as $AV){
$count++;
if($count != $AVMatchCriteriaCount){$extra = ',[';}else{$extra = '';}
$AVMatch = $AVMatch.'"'.$AV->case.'", "'.$AV->operator.'", "'.$AV->filter.'"]'.$extra;
}
$AVMatch = '['.$AVMatch;
最佳答案
echo 可以输出一个或多个字符串,而不是数组。
当你回显一个数组时,它应该会出现一个错误,如 Array to string conversion
。
但是在这里,在回显变量 $AVMatch 之后你得到了结果
["category", "=", "Server"],["siteid", "!=", "145228"],["udf8", "=", "asd"]
< br/>
这意味着它不是一个数组,而是一个字符串。
你必须传递一个像
这样的数组
[["category", "=", "Server"],["siteid", "!=", "145228"],["udf8", "=", "asd"]]
进入 where 子句。
但您传递的字符串看起来像 '["category", "=", "Server"],["siteid", "!=", "145228"],["udf8", "=", "作为
d"]'
希望你明白。
关于php - 拉维尔 5.8 : Trying to make a where model by using data pulled from database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57030738/