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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                > ## 查詢范圍:把基礎的查詢條件封裝成一個整體,在其他查詢中可以直接調用這個條件整體進行查詢 > 可以理解為把一串的鏈式操作定義為一個函數,查詢的時候直接利用鏈式進行調用 ### 查詢范圍定義 ``` class User extends Model { // 定義名為thinkphp的查詢范圍 protected function scopeThinkphp($query) { $query->where('name','thinkphp')->field('id,name'); } // 定義名為age的查詢范圍 protected function scopeAge($query) { $query->where('age','>',20)->limit(10); } } ``` ### 查詢范圍的調用 ``` // 查找name為thinkphp的用戶(基于thinkphp查詢范圍) User::scope('thinkphp')->find(); // 查找年齡大于20的10個用戶(基于age查詢范圍) User::scope('age')->select(); // 查找name為thinkphp的用戶并且年齡大于20的10個用戶(基于thinkphp和age查詢范圍) User::scope('thinkphp,age')->select(); // 也可以直接使用閉包函數(這種用法沒意義) User::scope(function($query){ $query->where('age','>',20)->limit(10); })->select(); // 動態調用(注意:查詢范圍必須在所有鏈式操作最前面調用,且后面必須為find或者select) $user = new User; // 查找name為thinkphp的用戶 (基于thinkphp查詢范圍) $user->thinkphp()->get(); // 查找年齡大于20的10個用戶 (基于age查詢范圍) $user->age()->all(); // 查找name為thinkphp的用戶并且年齡大于20的10個用戶 (基于thinkphp和age查詢范圍) $user->thinkphp()->age()->all(); ``` ### 傳入參數的查詢范圍 ``` // 定義方法 class User extends Model { protected function scopeAgeAbove($query, $lowest_age) { $query->where('age','>',$lowest_age)->limit(10); } } // 調用方法(必須為駝峰調用,不能為age_above) User::scope('ageAbove', 20)->select(); ``` ### 全局查詢范圍(模型中所有查詢操作都會自動調用這個查詢范圍) **全局查詢范圍方法在5.0.2版本之前必須定義為static靜態方法** ``` class User extends Model { // 定義全局的查詢范圍 protected function base($query) { $query->where('status',1); } } // 動態開關全局查詢范圍 // 關閉全局查詢范圍 User::useGlobalScope(false)->get(1); // 開啟全局查詢范圍 User::useGlobalScope(true)->get(2); ```
                  <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>

                              哎呀哎呀视频在线观看