为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查表是否已经在拉拉维尔查询生成器中具有 where 子句?

如何检查表是否已经在拉拉维尔查询生成器中具有 where 子句?

PHP
MYYA 2022-09-17 15:50:56
下面是一个示例,说明我们如何检测表是否已在 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我有一个具有一些条件和一些条件的查询,我需要在嵌套选择中使用其中一个条件。joinwherewhere现在我需要的是从查询中提取该 where 子句并再次使用它。问题是,如何确定我的查询是否包含 Eloquent 查询中表的特定列(例如 )的任何条件?whereorg.id我试图从查询中提取如下,但不像我们所拥有的连接wheres$wheres = $query->getQuery()->wheres;  foreach ($wheres as $where) {    dd(array_keys($where));  }我收到的是:array:3 [  0 => "type"  1 => "query"  2 => "boolean"]的值是,如果我尝试以下代码:typenested$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
查看完整描述

2 回答

?
慕盖茨4494581

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;

    }

  }


查看完整回答
反对 回复 2022-09-17
?
摇曳的蔷薇

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){

   ....

}


查看完整回答
反对 回复 2022-09-17
  • 2 回答
  • 0 关注
  • 241 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号