# 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();
~~~
- 基本說明
- 基本操作
- October cms 安裝
- 后臺控制器路徑
- 圖標
- 獲取安裝網上的插件/主題
- 插件構造器使用
- 定時任務
- October后臺控制器
- vscode編輯器
- ajax操作
- 使用
- ajax更新組件
- ajax屬性API
- JavaScript API
- ajax綜合使用
- 主題
- 多語言主題
- 安裝市場主題
- 主題程序處理
- 主題
- 頁面
- 部件
- 布局
- 內容
- 組件
- 媒體
- 主題表單操作
- 表單使用
- 表單后端程序處理
- 插件
- 自定義插件
- 插件說明
- 插件導航條
- 插件數據庫設置
- 插件的設置管理
- 插件的配置文件config
- 組件
- app服務
- app容器
- 擴展行為
- 緩存
- Collection類
- Lazy Collections
- Collection方法
- 助手函數
- 數組助手函數
- 路徑助手函數
- 玄樂助手函數
- 其他助手函數
- 錯誤與記錄
- 事件處理
- HTML頁面
- 文件與目錄操作
- 散列和加密
- 郵件
- 郵件內容
- 郵件發送
- 分頁
- 模板解析器
- 動態解析器語法
- 隊列消息
- 請求與輸入
- 響應
- 視圖
- 路由器
- 配置
- 驗證操作
- 處理錯誤消息
- 錯誤消息與視圖
- 可用的驗證規則
- 有條件的驗證規則
- 驗證數組
- 錯誤消息
- 自定義驗證規則
- 模型操作
- 定義模型與其屬性
- 檢索模型
- 插入與更新
- 刪除模型
- 查詢范圍
- 事件操作
- 關聯操作
- 定義關系
- 關系類型
- 多肽關系
- 關系查詢
- 渴望加載
- 插入模型
- 數據庫操作
- 基本用法
- 數據表結構
- 查詢連貫操作
- 結果檢索
- select子句
- 插入更新
- where子句
- 排序,分組,限制和偏移
- 文件附件
- Collection操作
- 屬性操作
- 系列化json
- 數據庫屬性
- 數據庫行為
- 控制器
- 后臺控制器定義
- 后臺頁面
- 后臺組件
- 后臺表單
- 表單組件
- 表單視圖
- 表單行為
- 后臺列表
- 列表行為
- 列表過濾器
- 可用列類型
- 關系行為
- 關系行為類型
- 擴展關系行為
- 列表排序操作
- 導入導出操作
- 用于與權限
- corlate模板修改
- 修改頂部導航
- laravel問題
- 控制器不存在
- 控制器
- 路由組
- laravel筆記
- laravel 安裝
- 偽靜態配置
- 依賴注入 & 控制器
- 中間件
- 路由文件
- 視圖