Skip to content

Commit

Permalink
Translate eloquent-collections.md (v5.5) (#380)
Browse files Browse the repository at this point in the history
* Translate eloquent-collections.md (v5.5)

* 同步原始文件最新一筆更新

* 12/7 修正部分翻譯問題
  • Loading branch information
Nationalcat authored and neighborhood999 committed Dec 8, 2017
1 parent 16f3381 commit 7eab1d7
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions eloquent-collections.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Eloquent: Collections
# Eloquent: 集合

- [Introduction](#introduction)
- [Available Methods](#available-methods)
- [Custom Collections](#custom-collections)
- [介紹](#introduction)
- [可用的方法](#available-methods)
- [自訂集合](#custom-collections)

<a name="introduction"></a>
## Introduction
## 介紹

All multi-result sets returned by Eloquent are instances of the `Illuminate\Database\Eloquent\Collection` object, including results retrieved via the `get` method or accessed via a relationship. The Eloquent collection object extends the Laravel [base collection](/docs/{{version}}/collections), so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.
Eloquent 回傳的所有多結果集都會是 `Illuminate\Database\Eloquent\Collection` 物件的實例,包括透過 `get` 方法取得或透過關聯存取的結果。Eloquent 集合物件繼承 Laravel [基礎集合](/docs/{{version}}/collections),所以它也自然的繼承了幾十種用於與底層 Eloquent 模型陣列的優雅方法。

Of course, all collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:
當然,所有的集合也都可以作為迭代器,可以讓你迭代他們就像是簡單的 PHP 陣列:

$users = App\User::where('active', 1)->get();

foreach ($users as $user) {
echo $user->name;
}

However, collections are much more powerful than arrays and expose a variety of map / reduce operations that may be chained using an intuitive interface. For example, let's remove all inactive models and gather the first name for each remaining user:
然而,集合比陣列強大許多,並提供 map reduce 等各種鏈結操作的直觀介面。例如,讓我們移除所有不活躍的模型,並收集其餘使用者的名字:

$users = App\User::where('active', 1)->get();

Expand All @@ -28,14 +28,14 @@ However, collections are much more powerful than arrays and expose a variety of
return $user->name;
});

> {note} While most Eloquent collection methods return a new instance of an Eloquent collection, the `pluck`, `keys`, `zip`, `collapse`, `flatten` and `flip` methods return a [base collection](/docs/{{version}}/collections) instance. Likewise, if a `map` operation returns a collection that does not contain any Eloquent models, it will be automatically cast to a base collection.
> {note} 雖然大多數 Eloquent 集合會回傳新的實例,不過只有 `pluck``keys``zip``collapse``flatten` `flip` 方法會回傳[基本的集合](/docs/{{version}}/collections)實例。同樣的,如果 `map` 操作會傳一個不具有任何 Eloquent 模型的集合,它會自動轉換成基本的集合。
<a name="available-methods"></a>
## Available Methods
## 可用的方法

### The Base Collection
### 基本的集合

All Eloquent collections extend the base [Laravel collection](/docs/{{version}}/collections) object; therefore, they inherit all of the powerful methods provided by the base collection class:
所有 Eloquent 集合都繼承了基本的 [Laravel 集合](/docs/{{version}}/collections)物件。因此,它們繼承了基本集合類別提供的所有強大方法:

<style>
#collection-method-list > p {
Expand Down Expand Up @@ -98,7 +98,7 @@ All Eloquent collections extend the base [Laravel collection](/docs/{{version}}/
[mode](/docs/{{version}}/collections#method-mode)
[nth](/docs/{{version}}/collections#method-nth)
[only](/docs/{{version}}/collections#method-only)
[pad](docs/{{version}}/collections#method-pad)
[pad](/docs/{{version}}/collections#method-pad)
[partition](/docs/{{version}}/collections#method-partition)
[pipe](/docs/{{version}}/collections#method-pipe)
[pluck](/docs/{{version}}/collections#method-pluck)
Expand Down Expand Up @@ -143,9 +143,9 @@ All Eloquent collections extend the base [Laravel collection](/docs/{{version}}/
</div>

<a name="custom-collections"></a>
## Custom Collections
## 自訂集合

If you need to use a custom `Collection` object with your own extension methods, you may override the `newCollection` method on your model:
如果你需要使用自己擴充的方法來自訂 `Collection` 物件,可以在你的模型上覆寫 `newCollection` 方法:

<?php

Expand All @@ -157,7 +157,7 @@ If you need to use a custom `Collection` object with your own extension methods,
class User extends Model
{
/**
* Create a new Eloquent Collection instance.
* 建立一個新的 Eloquent 集合實例。
*
* @param array $models
* @return \Illuminate\Database\Eloquent\Collection
Expand All @@ -168,4 +168,4 @@ If you need to use a custom `Collection` object with your own extension methods,
}
}

Once you have defined a `newCollection` method, you will receive an instance of your custom collection anytime Eloquent returns a `Collection` instance of that model. If you would like to use a custom collection for every model in your application, you should override the `newCollection` method on a base model class that is extended by all of your models.
你一旦定義了 `newCollection` 方法,在任何 Eloquent 回傳該模型的 `Collection` 實例的時候,你會接收到一個你的自訂集合的實例。如果你想要在應用程式中為每個模型使用自訂的集合,你應該在所有模型繼承的基本模型類別上覆寫 `newCollection` 方法。

0 comments on commit 7eab1d7

Please sign in to comment.