<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                我使用 Laravel 開發已經半年多,兩個項目。Laravel 完善的文檔,以及強大的社區,幾乎可以應付所有疑問,這使我很少去看低層代碼實現邏輯。盡管能很好的實現邏輯,但是對代碼的把控還不是很好,就在前幾天,我需要寫一個帶有 OR 條件的查詢,當時讓我煞費苦心,我搜索了很多信息,都沒有查到,所有關于稍微復雜一點的 OR 查詢都是在講解這個匿名函數實現: ~~~php DB::table('users') ->where('name', '=', 'John') ->where(function ($query) { $query->where('votes', '>', 100) ->orWhere('title', '=', 'Admin'); }) ->get(); ~~~ 而這并不能滿足我,也許是我的關鍵詞不對,沒有找到那個完美的答案,并且我想要更多更靈活的方式。 我要實現的 SQL 大概是這樣的: ~~~sql SELECT * FROM user WHERE group_id = 'group id' AND ( name = 'name' OR mobile_number = 'mobile number' OR email = 'email' OR `score` > 1000 ) ~~~ 這是一類很常見的業務邏輯,可能你會覺得很簡單,我也知道怎么去實現: ~~~php DB::table('users') ->where('group_id', 'group id') ->where(function ($query) { $query->where('name', 'name') ->orWhere('mobile_number', '=', 'mobile number') ->orWhere('email', '=', 'email') ->orWhere('score', '>', '1000'); }) ->get(); ~~~ 但是實際的邏輯并不是這樣的,我還需要去判斷是否有對應的參數,才需要把對應查詢條件寫入,就像這樣: ~~~php DB::table('users') ->where('group_id', 'group id') ->where(function ($query) { if ($params['name']) { $query->orWhere('name', $params['name']) } if ($params['mobile_number']) { $query->orWhere('mobile_number', $params['mobile_number']) } if ($params['email']) { $query->orWhere('email', $params['email']) } if ($params['score']) { $query->orWhere('score', '>', $params['score']) } }) ->get(); ~~~ 我知道這可行,一直都是這樣寫的,但我覺得強大的 Laravel 肯定不會僅僅提供這種方式,于是我決定看一眼低層代碼,很幸運,我有新的發現: ~~~php /** * Add a basic where clause to the query. * * @param \Closure|string|array $column * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function where($column, $operator = null, $value = null, $boolean = 'and') { // If the column is an array, we will assume it is an array of key-value pairs // and can add them each as a where clause. We will maintain the boolean we // received when the method was called and pass it into the nested where. if (is_array($column)) { return $this->addArrayOfWheres($column, $boolean); } // Rest of code ... } /** * Add an array of where clauses to the query. * * @param array $column * @param string $boolean * @param string $method * @return $this */ protected function addArrayOfWheres($column, $boolean, $method = 'where') { return $this->whereNested(function ($query) use ($column, $method, $boolean) { foreach ($column as $key => $value) { if (is_numeric($key) && is_array($value)) { $query->{$method}(...array_values($value)); } else { $query->$method($key, '=', $value, $boolean); } } }, $boolean); } ~~~ `where`方法中,我只保留了需要重點關注的一段代碼,如果條件滿足,將會進入`addArrayOfWheres`方法,在這里解析以數組方式傳遞進來的條件參數,并且該組條件會被分組,也就是會用`()`包起來。有兩種方式可以讓查詢條件分組,一是數組傳參,二是匿名函數。類似我這種 OR 條件的查詢,關鍵的就是讓查詢正確分組。 另外一個關鍵代碼: ~~~php if (is_numeric($key) && is_array($value)) { $query->{$method}(...array_values($value)); } ~~~ 數組參數會被展開,看到這個,我想我的代碼可以寫成這樣: ~~~php $orWhere = []; if ($params['name']) { $orWhere[] = ['name', '=', $params['name'], 'OR']; } if ($params['mobile_number']) { $orWhere[] = ['mobile_number', '=', $params['mobile_number'], 'OR']; } if ($params['email']) { $orWhere[] = ['email', '=', $params['email'], 'OR']; } if ($params['score']) { $orWhere[] = ['score', '>', $params['score'], 'OR']; } DB::table('users') ->where('group_id', 'group id') ->where($orWhere) ->get(); ~~~ `$orWhere`會被分組且被`...array_values($value)`展開。 也可以這樣: ~~~php $orWhere = []; if ($params['name']) { $orWhere['name'] = $params['name']; } if ($params['mobile_number']) { $orWhere['mobile_number'] = $params['mobile_number']; } if ($params['email']) { $orWhere['email'] = $params['email']; } if ($params['score']) { $orWhere[] = ['score', '>', 1000, 'OR']; } DB::table('users') ->where('group_id', 'group id') ->where(function ($query) use ($orWhere) { $query->orWhere($orWhere); }) ->get(); ~~~ 最終的 sql 是這樣的 ~~~sql select * from `users` where `group_id` = 'group id' and ( ( `name` = 'name' or `mobile_number` = 'mobile number' or `email` = 'email' OR `score` > 1000 ) ) ~~~ 雖然很多人都知道這個,我還是希望這能帶來些許啟發。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看