【发布时间】:2019-02-09 13:24:13
【问题描述】:
有没有办法将多个 where 条件传递到 Laravel 的集合中?我的意思是:
$filters = [['col1', '=', 'val1'], ['col2', '=', 'val2']];
$found = $collection->where($filters)->first();
我知道它适用于 eloquent 查询构建器,但不适用于集合。我知道我可以链接多个 ->where 语句(即使在 foreach 循环中),但我需要拥有原始集合对象,并且克隆它可以工作,但速度不是那么快。
$localCollectionObject = $localCollection->first(function ($value, $key) use ($remoteCollectionObject, $compareColumnNames) {
foreach ($compareColumnNames as $compareColumnName) {
if ($value->{$compareColumnName} != $remoteCollectionObject[$compareColumnName]) {
return false;
}
}
return true;
});
这也很好用,但比clone $localCollection 还要慢。
或者我可以以某种方式“重置” where 语句?
我在 foreach 循环中以不同的条件搜索它,这是一个问题。
【问题讨论】:
-
“克隆”与过滤任何东西有什么关系?
-
都在foreach循环内,条件不同,所以我需要在原始集合中搜索,没有
where。
标签: php laravel collections