<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之旅 廣告
                # 檢索模型 ### [](https://octobercms.com/docs/database/model#retrieving-models)檢索模型 從數據庫請求數據時,模型將主要使用`get`或`first`方法檢索值,具體取決于您是希望[檢索多個模型](https://octobercms.com/docs/database/model#retrieving-multiple-models)還是[檢索單個模型](https://octobercms.com/docs/database/model#retrieving-single-models)。從模型派生的查詢返回[October \\ Rain \\ Database \\ Builder](https://octobercms.com/docs/api/october/rain/database/builder)的實例。 > **注意**:默認情況下,所有模型查詢都[啟用](https://octobercms.com/docs/database/query#in-memory-caching)了[內存中緩存](https://octobercms.com/docs/database/query#in-memory-caching)。盡管緩存在大多數情況下會自動使自身失效,但是`$model->reload()`對于更復雜的用例,有時您將需要使用該方法來刷新緩存。 ### [](https://octobercms.com/docs/database/model#retrieving-multiple-models)檢索多個模型 創建模型[及其關聯的數據庫表之后](https://octobercms.com/docs/database/structure#migration-structure),就可以開始從數據庫中檢索數據了。將每個模型視為強大的[查詢生成器,](https://octobercms.com/docs/database/query)使您可以查詢與該模型關聯的數據庫表。例如: ~~~ $flights = Flight::all(); ~~~ #### [](https://octobercms.com/docs/database/model#accessing-column-values)訪問列值 如果您有模型實例,則可以通過訪問相應的屬性來訪問模型的列值。例如,讓我們遍歷`Flight`查詢返回的每個實例,并回顯該`name`列的值: ~~~ foreach ($flights as $flight) { echo $flight->name; } ~~~ #### [](https://octobercms.com/docs/database/model#adding-constraints)添加其他約束 該`all`方法將返回模型表中的所有結果。由于每個模型都充當[查詢構建器](https://octobercms.com/docs/database/query),因此您還可以向查詢添加約束,然后使用該`get`方法來檢索結果: ~~~ $flights = Flight::where('active', 1) ->orderBy('name', 'desc') ->take(10) ->get(); ~~~ > **注意:**由于模型是查詢構建器,因此您應該熟悉[查詢構建器](https://octobercms.com/docs/database/query)上可用的所有方法。您可以在模型查詢中使用這些方法中的任何一種。 #### [](https://octobercms.com/docs/database/model#returning-collections)館藏 對于`all`和`get`檢索多個結果的方法,`Collection`將返回a的實例。此類提供[了各種有用的方法](https://octobercms.com/docs/database/collection)來處理結果。當然,您可以像數組一樣簡單地遍歷此集合: ~~~ foreach ($flights as $flight) { echo $flight->name; } ~~~ #### [](https://octobercms.com/docs/database/model#chunking-results)分塊結果 如果需要處理數千條記錄,請使用`chunk`命令。該`chunk`方法將檢索模型的“塊”,將其饋給給定的模型`Closure`進行處理。使用`chunk`大型結果集時,使用該方法將節省內存: ~~~ Flight::chunk(200, function ($flights) { foreach ($flights as $flight) { // } }); ~~~ 傳遞給該方法的第一個參數是每個“塊”希望接收的記錄數。從數據庫中檢索到的每個塊都將調用作為第二個參數傳遞的Closure。 ### [](https://octobercms.com/docs/database/model#retrieving-single-models)檢索單個模型 除了檢索給定表的所有記錄之外,您還可以使用`find`和檢索單個記錄`first`。這些方法返回一個模型實例,而不是返回模型集合: ~~~ // Retrieve a model by its primary key $flight = Flight::find(1); // Retrieve the first model matching the query constraints $flight = Flight::where('active', 1)->first(); ~~~ #### [](https://octobercms.com/docs/database/model#model-not-found-exception)找不到例外 有時,如果找不到模型,您可能希望拋出異常。這在路由或控制器中特別有用。該`findOrFail`和`firstOrFail`方法將檢索查詢的第一個結果。但是,如果未找到結果,`Illuminate\Database\Eloquent\ModelNotFoundException`將拋出: ~~~ $model = Flight::findOrFail(1); $model = Flight::where('legs', '>', 100)->firstOrFail(); ~~~ 當[開發一個API](https://octobercms.com/docs/services/router),如果異常沒有被捕獲,一個`404`HTTP響應被自動發送回用戶,所以沒有必要寫明確檢查返回`404`使用這些方法時的反應: ~~~ Route::get('/api/flights/{id}', function ($id) { return Flight::findOrFail($id); }); ~~~ ### [](https://octobercms.com/docs/database/model#retrieving-aggregates)檢索聚合 您也可以使用`count`,`sum`,`max`,和其他[聚合函數](https://octobercms.com/docs/database/query#aggregates)通過查詢器提供。這些方法返回適當的標量值,而不是完整的模型實例: ~~~ $count = Flight::where('active', 1)->count(); $max = Flight::where('active', 1)->max('price'); ~~~
                  <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>

                              哎呀哎呀视频在线观看