2 回答
TA贡献1850条经验 获得超11个赞
您可以对查询生成器的属性执行几乎相同的操作。它将包含一个具有以下格式的 where 子句数组:->wheres
["type"] => string(5) "Basic"
["column"] =>string(1) "q"
["operator"] => string(1) "="
["value"] => int(2)
["boolean"] => string(3) "and"
从这一点来看,列属性似乎是您可以使用的。
UPD:基本上,您可以创建一个递归函数来为您检查它:
...
$wheres = $query->getQuery()->wheres;
foreach ($wheres as $where) {
if( $checkColumn($where, 'myColumn) ) {
// found it
break;
}
}
function checkColumn($where, $column) {
if( $where['type'] == 'nested' ) {
return checkColumn($where['query'], $column);
}
else {
return $where['column'] == $column;
}
}
TA贡献1793条经验 获得超6个赞
以下是我的递归函数,可以放置在 Model 类中:
/**
* Returns the where conditions on a specific joined table?
* @return array of wheres
*/
protected function wheres($query, $table){
$conditions = [];
if($query instanceof \Illuminate\Database\Eloquent\Builder){
$wheres = $query->getQuery()->wheres;
} elseif($query instanceof \Illuminate\Database\Query\Builder) {
$wheres = $query->wheres;
}
if($wheres == null) {
return [];
}
foreach ($wheres as $where) {
if (isset($where['column']) && substr_compare($where['column'], $table, 0, strlen($table)) === 0){
array_push($conditions, $where);
} else if (isset($where['query'])){
$conditions = array_merge($conditions, $this->wheres($where['query'], $table));
}
}
return $conditions;
}
可以在控制器中调用它,如下所示:
$wheres = $this->wheres($query, 'org');
foreach($wheres as $where){
....
}
- 2 回答
- 0 关注
- 241 浏览
添加回答
举报
