<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之旅 廣告
                # Collection操作 ### 介紹 模型返回的所有多結果集都是`Illuminate\Database\Eloquent\Collection`對象的實例,包括通過`get`方法檢索或通過關系訪問的結果。該`Collection`對象擴展了[基礎集合](https://octobercms.com/docs/services/collections),因此它自然地繼承了數十種用于流暢地使用基礎模型數組的方法。 所有集合還充當迭代器,使您可以像遍歷簡單的PHP數組一樣遍歷它們: ~~~ $users = User::where('is_active', true)->get(); foreach ($users as $user) { echo $user->name; } ~~~ 但是,集合比數組強大得多,并且使用直觀的界面公開了各種map / reduce操作。例如,讓我們過濾所有活動模型并收集每個過濾用戶的名稱: ~~~ $users = User::get(); $names = $users->filter(function ($user) { return $user->is_active === true; }) ->map(function ($user) { return $user->name; }); ~~~ > **注:**雖然大多數模型收集方法返回的新實例`Eloquent`收藏,`pluck`,`keys`,`zip`,`collapse`,`flatten`和`flip`方法返回一個基本集合實例。同樣,如果某個`map`操作返回的集合不包含任何模型,則它將自動轉換為基本集合。 ### [](https://octobercms.com/docs/database/collection#usage-examples)可用方法 所有模型集合都擴展了基礎集合對象。因此,它們繼承了基本集合類提供的所有強大方法。 此外,`Illuminate\Database\Eloquent\Collection`該類還提供了方法的超集,以幫助管理模型集合。大多數方法返回`Illuminate\Database\Eloquent\Collection`實例;但是,某些方法返回一個基本`Illuminate\Support\Collection`實例。 **contains($key, $operator = null, $value = null)** 該`contains`方法可以用于確定集合是否包含給定的模型實例。此方法接受主鍵或模型實例: ~~~ $users->contains(1); $users->contains(User::find(1)); ~~~ **diff($items)** 該`diff`方法返回給定集合中不存在的所有模型: ~~~ use App\User; $users = $users->diff(User::whereIn('id', [1, 2, 3])->get()); ~~~ **except($keys)** 該`except`方法返回所有沒有給定主鍵的模型: ~~~ $users = $users->except([1, 2, 3]); ~~~ **find($key)** 該`find`方法找到具有給定主鍵的模型。如果`$key`是模型實例,`find`將嘗試返回與主鍵匹配的模型。如果`$key`是鍵數組,則find將返回與`$keys`using匹配的所有模型`whereIn()`: ~~~ $users = User::all(); $user = $users->find(1); ~~~ **fresh($with = [])** 該`fresh`方法從數據庫中檢索集合中每個模型的新實例。另外,任何指定的關系都將被加載: ~~~ $users = $users->fresh(); $users = $users->fresh('comments'); ~~~ **intersect($items)** 該`intersect`方法返回給定集合中也存在的所有模型: ~~~ use App\User; $users = $users->intersect(User::whereIn('id', [1, 2, 3])->get()); ~~~ **load($relations)** `load`急于加載集合中所有模型的給定關系的方法: ~~~ $users->load('comments', 'posts'); $users->load('comments.author'); ~~~ **loadMissing($relations)** `loadMissing`如果尚未加載關系,則該方法渴望為集合中的所有模型加載給定的關系: ~~~ $users->loadMissing('comments', 'posts'); $users->loadMissing('comments.author'); ~~~ **modelKeys()** 該`modelKeys`方法返回集合中所有模型的主鍵: ~~~ $users->modelKeys(); // [1, 2, 3, 4, 5] ~~~ **makeVisible($attributes)** 該`makeVisible`方法使可見的屬性通常在集合中的每個模型上“隱藏”: ~~~ $users = $users->makeVisible(['address', 'phone_number']); ~~~ **makeHidden($attributes)** 該`makeHidden`方法隱藏通常在集合中的每個模型上“可見”的屬性: ~~~ $users = $users->makeHidden(['address', 'phone_number']); ~~~ **only($keys)** 該`only`方法返回具有給定主鍵的所有模型: ~~~ $users = $users->only([1, 2, 3]); ~~~ **unique($key = null,$strict = false)** 該`unique`方法返回集合中的所有唯一模型。與集合中另一個模型具有相同主鍵的相同類型的所有模型都將被刪除。 ~~~ $users = $users->unique(); ~~~ ### [](https://octobercms.com/docs/database/collection#custom-collections)定制收藏 如果需要將自定義`Collection`對象與自己的擴展方法一起使用,則可以`newCollection`在模型上覆蓋該方法: ~~~ class User extends Model { /** * Create a new Collection instance. */ public function newCollection(array $models = []) { return new CustomCollection($models); } } ~~~ 定義`newCollection`方法后,只要模型返回`Collection`實例,您將收到自定義集合的實例。如果要對插件或應用程序中的每個模型使用自定義集合,則應覆蓋`newCollection`由所有模型擴展的模型基類上的方法。 ~~~ use October\Rain\Database\Collection as CollectionBase; class CustomCollection extends CollectionBase { } ~~~ ### [](https://octobercms.com/docs/database/collection#data-feed)數據饋送 數據提要允許您將多個模型類組合為一個集合。這對于創建提要和數據流同時支持分頁使用很有用。它的工作`get`方式是在調用方法之前,以準備好的狀態添加模型對象,然后將這些對象組合在一起以構成一個行為與常規數據集相同的集合。 本`DataFeed`類模仿一個普通類型,可以支持`limit`和`paginate`方法。 ### [](https://octobercms.com/docs/database/collection#creating-feed)創建一個新的提要 下一個示例將User,Post和Comment模型組合為一個集合,并返回前10條記錄。 ~~~ $feed = new October\Rain\Database\DataFeed; $feed->add('user', new User); $feed->add('post', Post::where('category_id', 7)); $feed->add('comment', function() { $comment = new Comment; return $comment->where('approved', true); }); $results = $feed->limit(10)->get(); ~~~ ### [](https://octobercms.com/docs/database/collection#data-feed-processing)處理結果 該`get`方法將返回一個`Collection`包含結果的對象。可以使用`tag_name`添加模型時設置為第一個參數的屬性來區分記錄。 ~~~ foreach ($results as $result) { if ($result->tag_name == 'post') echo "New Blog Post: " . $record->title; elseif ($result->tag_name == 'comment') echo "New Comment: " . $record->content; elseif ($result->tag_name == 'user') echo "New User: " . $record->name; } ~~~ ### [](https://octobercms.com/docs/database/collection#data-feed-ordering)訂購結果 可以通過單個數據庫列對結果進行排序,該列可以是所有數據集使用的共享默認值,也可以使用`add`方法單獨指定。結果的方向也必須共享。 ~~~ // Ordered by updated_at if it exists, otherwise created_at $feed->add('user', new User, 'ifnull(updated_at, created_at)'); // Ordered by id $feed->add('comments', new Comment, 'id'); // Ordered by name (specified default below) $feed->add('posts', new Post); // Specifies the default column and the direction $feed->orderBy('name', 'asc')->get(); ~~~
                  <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>

                              哎呀哎呀视频在线观看