php - 如何检查表是否已经在 Laravel Query Builder 中有 where 子句?
问题描述
这是一个示例,说明我们如何检测表是否已加入 Laravel 的查询中。
public static function isJoined($query, $table)
{
$joins = $query->getQuery()->joins;
if($joins == null) {
return false;
}
foreach ($joins as $join) {
if ($join->table == $table) {
return true;
}
}
return false;
}
我需要的是提取where
查询中的子句。
我有一个具有join
一些where
条件的查询,我需要where
在嵌套选择中使用其中一个条件。
现在我需要从查询中提取 where 子句并再次使用它。问题是如何确定我的查询是否包含我的 Eloquent 查询where
中表的特定列(例如)的任何条件?org.id
我试图wheres
从查询中提取如下,但不像我们所拥有的连接
$wheres = $query->getQuery()->wheres;
foreach ($wheres as $where) {
dd(array_keys($where));
}
我收到的是:
array:3 [
0 => "type"
1 => "query"
2 => "boolean"
]
的值type
是nested
,如果我尝试以下代码:
$wheres = $query->getQuery()->wheres;
foreach ($wheres as $where) {
dd($where['query']->wheres);
}
然后我有:
array:1 [
0 => array:5 [
"type" => "Basic"
"column" => "org.path"
"operator" => "LIKE"
"value" => "/202001/10000000/12400000%"
"boolean" => "and"
]
]
现在为什么第一个wheres
返回不同的对象?我首先想到了这个结果$where
!
解决方案
->wheres
您可以对查询生成器的属性执行几乎相同的操作。它将包含以下格式的 where 子句数组:
["type"] => string(5) "Basic"
["column"] =>string(1) "q"
["operator"] => string(1) "="
["value"] => int(2)
["boolean"] => string(3) "and"
由此看来,您可以使用column属性。
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;
}
}
推荐阅读
- java - 在 HTTP 503 上重新建立 TCP 连接
- python - DataFrame 到 OrderedDicts 列表 - 如何保留订单?
- php - 通过 API 从 Google Cloud 访问 YouTube 帐户以获取统计信息
- asp.net-core - Web API POST - 在邮递员中给出 500 错误 - 如何解决?
- reactjs - 如何将今天的日期设置为 Material UI 日期选择器的默认日期
- php - WordPress 5.5.3。致命错误:无法在 ...\wp-include 中重新声明 _wp_register_meta_args_whitelist()(之前在 ...\wp-includes\meta.php:1409 中声明)
- r - 如何在循环中将向量拆分为更小的向量?
- ios - youtube视频没有在ios中开始
- azure - 为 Azure Kafka 配置灾难恢复和自动故障转移?
- raspberry-pi - 是否可以在不登录的情况下通过 ssh 进入 Raspberry PI?