Skip to content

Commit fd4a8e3

Browse files
committed
translate complete
1 parent 5cc45d8 commit fd4a8e3

File tree

5 files changed

+490
-500
lines changed

5 files changed

+490
-500
lines changed

eloquent-collections.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Laravel 的 Eloquent 集合
1+
# Eloquent: 集合
22

3-
- [Introduction](#introduction)
4-
- [Available Methods](#available-methods)
5-
- [Custom Collections](#custom-collections)
3+
- [简介](#introduction)
4+
- [可用的方法](#available-methods)
5+
- [自定义集合](#custom-collections)
66

77
<a name="introduction"></a>
8-
## Introduction
8+
## 简介
99

10-
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.
10+
默认情况下 Eloquent 返回的都是一个 `Illuminate\Database\Eloquent\Collection` 对象的实例,包含通过 `get` 方法或是访问一个关联来获取到的结果。Eloquent 集合对象继承了 Laravel [集合基类](/docs/{{version}}/collections),因此它自然也继承了许多可用于与 Eloquent 模型交互的方法。
1111

12-
Of course, all collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:
12+
当然,所有集合都可以作为迭代器,来让你像遍历一个 PHP 数组一样来遍历一个集合:
1313

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

1616
foreach ($users as $user) {
1717
echo $user->name;
1818
}
1919

20-
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:
20+
然而,集合比数组更强大的地方是其使用了各种 map / reduce 的直观操作。例如,我们移除所有未激活的用户模型和收集其余各个用户的名字:
2121

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

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

31-
> {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.
31+
> {note} 大部分的 Eloquent 集合会返回新的「Eloquent 集合」实例,但是 `pluck`, `keys`, `zip`, `collapse`, `flatten``flip` 方法会返回 [基础集合](/docs/{{version}}/collections) 实例。
32+
>
33+
> 相应的,如果一个 `map` 操作返回一个不包含任何 Eloquent 模型的集合,那么它将会自动转换成基础集合。
34+
3235

3336
<a name="available-methods"></a>
34-
## Available Methods
37+
## 可用的方法
3538

36-
### The Base Collection
39+
### 集合对象
3740

38-
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:
41+
所有 Eloquent 集合都继承了基础的 [Laravel 集合](/docs/{{version}}/collections) 对象。因此,他们也继承了所有集合类提供的强大的方法:
3942

4043
<style>
4144
#collection-method-list > p {
@@ -118,9 +121,10 @@ All Eloquent collections extend the base [Laravel collection](/docs/{{version}}/
118121
</div>
119122

120123
<a name="custom-collections"></a>
121-
## Custom Collections
124+
## 自定义集合
125+
122126

123-
If you need to use a custom `Collection` object with your own extension methods, you may override the `newCollection` method on your model:
127+
如果你需要使用一个自定义的 `Collection` 对象到自己的扩充方法上,则可以在模型中重写 `newCollection` 方法:
124128

125129
<?php
126130

@@ -132,7 +136,7 @@ If you need to use a custom `Collection` object with your own extension methods,
132136
class User extends Model
133137
{
134138
/**
135-
* Create a new Eloquent Collection instance.
139+
* 创建一个新的 Eloquent 集合实例对象。
136140
*
137141
* @param array $models
138142
* @return \Illuminate\Database\Eloquent\Collection
@@ -143,4 +147,5 @@ If you need to use a custom `Collection` object with your own extension methods,
143147
}
144148
}
145149

146-
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.
150+
151+
一旦你定义了 `newCollection` 方法,则可在任何 Eloquent 返回该模型的 `Collection` 实例时,接收到一个你的自定义集合的实例。如果你想要在应用程序的每个模型中使用自定义集合,则应该在所有的模型继承的模型基类中重写 `newCollection` 方法。

eloquent-mutators.md

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
# Laravel 的 Eloquent 修改器
1+
# Eloquent: 修改器
22

3-
- [Introduction](#introduction)
4-
- [Accessors & Mutators](#accessors-and-mutators)
5-
- [Defining An Accessor](#defining-an-accessor)
6-
- [Defining A Mutator](#defining-a-mutator)
7-
- [Date Mutators](#date-mutators)
8-
- [Attribute Casting](#attribute-casting)
9-
- [Array & JSON Casting](#array-and-json-casting)
3+
- [简介](#introduction)
4+
- [访问器 & 修改器](#accessors-and-mutators)
5+
- [定义一个访问器](#defining-an-accessor)
6+
- [定义一个修改器](#defining-a-mutator)
7+
- [日期转换器](#date-mutators)
8+
- [属性类型转换](#attribute-casting)
9+
- [数组 & JSON 转换](#array-and-json-casting)
1010

1111
<a name="introduction"></a>
12-
## Introduction
12+
## 简介
1313

14-
Accessors and mutators allow you to format Eloquent attribute values when you retrieve or set them on model instances. For example, you may want to use the [Laravel encrypter](/docs/{{version}}/encryption) to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model.
14+
访问器和修改器可以让你修改 Eloquent 模型中的属性或者设置它们的值,例如,你可能想要使用 [Laravel 加密器](/docs/{{version}}/encryption) 来加密一个被保存在数据库中的值,当你从 Eloquent 模型访问该属性时该值将被自动解密。
1515

16-
In addition to custom accessors and mutators, Eloquent can also automatically cast date fields to [Carbon](https://github.com/briannesbitt/Carbon) instances or even [cast text fields to JSON](#attribute-casting).
16+
除了自定义访问器和修改器之外,Eloquent 也会自动将日期字段类型转换成 [Carbon](https://github.com/briannesbitt/Carbon) 实例或将 [文本字段类型转换成 JSON](#attribute-casting)
1717

1818
<a name="accessors-and-mutators"></a>
19-
## Accessors & Mutators
19+
## 访问器 & 修改器
2020

2121
<a name="defining-an-accessor"></a>
22-
### Defining An Accessor
22+
### 定义一个访问器
2323

24-
To define an accessor, create a `getFooAttribute` method on your model where `Foo` is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the `first_name` attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of the `first_name` attribute:
24+
若要定义一个访问器,则须在你的模型上创建一个 `getFooAttribute` 方法。要访问的 `Foo` 字段需使用「驼峰式」来命名。在这个例子中,我们将为 `first_name` 属性定义一个访问器。当 Eloquent 尝试获取 `first_name` 的值时,将会自动调用此访问器:
2525

2626
<?php
2727

@@ -32,7 +32,7 @@ To define an accessor, create a `getFooAttribute` method on your model where `Fo
3232
class User extends Model
3333
{
3434
/**
35-
* Get the user's first name.
35+
* 获取用户的名字。
3636
*
3737
* @param string $value
3838
* @return string
@@ -43,16 +43,16 @@ To define an accessor, create a `getFooAttribute` method on your model where `Fo
4343
}
4444
}
4545

46-
As you can see, the original value of the column is passed to the accessor, allowing you to manipulate and return the value. To access the value of the accessor, you may simply access the `first_name` attribute on a model instance:
46+
如你所见,字段的原始值被传递到访问器中,让你可以编辑修改并返回结果。如果要访问被修改的值,则可以像这样来访问 `first_name` 属性:
4747

4848
$user = App\User::find(1);
4949

5050
$firstName = $user->first_name;
5151

5252
<a name="defining-a-mutator"></a>
53-
### Defining A Mutator
53+
### 定义一个修改器
5454

55-
To define a mutator, define a `setFooAttribute` method on your model where `Foo` is the "studly" cased name of the column you wish to access. So, again, let's define a mutator for the `first_name` attribute. This mutator will be automatically called when we attempt to set the value of the `first_name` attribute on the model:
55+
若要定义一个修改器,则须在模型上定义一个 `setFooAttribute` 方法。要访问的 `Foo` 字段需使用「驼峰式」来命名。让我们再来定义 `first_name` 属性的修改器。当我们尝试在模型上设置 `first_name` 的值时,将会自动调用此修改器:
5656

5757
<?php
5858

@@ -63,7 +63,7 @@ To define a mutator, define a `setFooAttribute` method on your model where `Foo`
6363
class User extends Model
6464
{
6565
/**
66-
* Set the user's first name.
66+
* 设定用户的名字。
6767
*
6868
* @param string $value
6969
* @return void
@@ -74,18 +74,20 @@ To define a mutator, define a `setFooAttribute` method on your model where `Foo`
7474
}
7575
}
7676

77-
The mutator will receive the value that is being set on the attribute, allowing you to manipulate the value and set the manipulated value on the Eloquent model's internal `$attributes` property. So, for example, if we attempt to set the `first_name` attribute to `Sally`:
77+
修改器会获取属性已经被设置的值,让你可以操作该值并将其设置到 Eloquent 模型内部的 `$attributes` 属性上。举个例子,如果我们尝试将 `first_name` 属性设置成 `Sally`
7878

7979
$user = App\User::find(1);
8080

8181
$user->first_name = 'Sally';
8282

83-
In this example, the `setFirstNameAttribute` function will be called with the value `Sally`. The mutator will then apply the `strtolower` function to the name and set its resulting value in the internal `$attributes` array.
83+
在这个例子中,`setFirstNameAttribute` 函数将会使用 `Sally` 作为参数来调用。修改器会对该名字使用 `strtolower` 函数并将其值设置于内部的 `$attributes` 数组。
8484

8585
<a name="date-mutators"></a>
86-
## Date Mutators
86+
## 日期转换器
8787

88-
By default, Eloquent will convert the `created_at` and `updated_at` columns to instances of [Carbon](https://github.com/briannesbitt/Carbon), which extends the PHP `DateTime` class to provide an assortment of helpful methods. You may customize which dates are automatically mutated, and even completely disable this mutation, by overriding the `$dates` property of your model:
88+
默认情况下,Eloquent 将会把 `created_at``updated_at` 字段转换成 [Carbon](https://github.com/briannesbitt/Carbon) 实例,它提供了各种各样的方法,并继承了 PHP 原生的 DateTime 类。
89+
90+
你可以在模型中自定义哪些字段需要被自动修改,或完全禁止修改,可通过重写模型的 `$dates` 属性来实现:
8991

9092
<?php
9193

@@ -96,7 +98,7 @@ By default, Eloquent will convert the `created_at` and `updated_at` columns to i
9698
class User extends Model
9799
{
98100
/**
99-
* The attributes that should be mutated to dates.
101+
* 应被转换为日期的属性。
100102
*
101103
* @var array
102104
*/
@@ -107,23 +109,23 @@ By default, Eloquent will convert the `created_at` and `updated_at` columns to i
107109
];
108110
}
109111

110-
When a column is considered a date, you may set its value to a UNIX timestamp, date string (`Y-m-d`), date-time string, and of course a `DateTime` / `Carbon` instance, and the date's value will automatically be correctly stored in your database:
112+
当某个字段被认为是日期时,你或许想将其数值设置成一个 UNIX 时间戳、日期字符串(`Y-m-d`)、日期时间( `date-time` )字符串、当然还有 `DateTime` `Carbon` 实例,然后日期数值将会被自动保存到数据库中:
111113

112114
$user = App\User::find(1);
113115

114116
$user->deleted_at = Carbon::now();
115117

116118
$user->save();
117119

118-
As noted above, when retrieving attributes that are listed in your `$dates` property, they will automatically be cast to [Carbon](https://github.com/briannesbitt/Carbon) instances, allowing you to use any of Carbon's methods on your attributes:
120+
如上所述,在 `$dates` 属性中列出的所有属性被获取到时,都将会自动转换成 [Carbon](https://github.com/briannesbitt/Carbon) 实例,让你可在属性上使用任何 `Carbon` 方法:
119121

120122
$user = App\User::find(1);
121123

122124
return $user->deleted_at->getTimestamp();
123125

124-
#### Date Formats
126+
#### 时间格式
125127

126-
By default, timestamps are formatted as `'Y-m-d H:i:s'`. If you need to customize the timestamp format, set the `$dateFormat` property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:
128+
默认情况下,时间戳将会以 `'Y-m-d H:i:s'` 格式化。如果你想要自定义自己的时间戳格式,可在模型中设置 `$dateFormat` 属性。该属性定义了时间属性应如何被保存到数据库,以及模型应被序列化成一个数组或 JSON 格式:
127129

128130
<?php
129131

@@ -134,19 +136,32 @@ By default, timestamps are formatted as `'Y-m-d H:i:s'`. If you need to customiz
134136
class Flight extends Model
135137
{
136138
/**
137-
* The storage format of the model's date columns.
139+
* 模型的数据字段的保存格式。
138140
*
139141
* @var string
140142
*/
141143
protected $dateFormat = 'U';
142144
}
143145

144146
<a name="attribute-casting"></a>
145-
## Attribute Casting
147+
## 属性类型转换
148+
149+
`$casts` 属性在模型中提供了将属性转换为常见的数据类型的方法。`$casts` 属性应是一个数组,且键是那些需要被转换的属性名称,值则是代表字段要转换的类型。支持的转换的类型有:
146150

147-
The `$casts` property on your model provides a convenient method of converting attributes to common data types. The `$casts` property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are: `integer`, `real`, `float`, `double`, `string`, `boolean`, `object`, `array`, `collection`, `date`, `datetime`, and `timestamp`.
151+
+ integer
152+
+ real
153+
+ float
154+
+ double
155+
+ string
156+
+ boolean
157+
+ object
158+
+ array
159+
+ collection
160+
+ date
161+
+ datetime
162+
+ timestamp
148163

149-
For example, let's cast the `is_admin` attribute, which is stored in our database as an integer (`0` or `1`) to a boolean value:
164+
例如,`is_admin` 属性以整数(`0` `1`)被保存在我们的数据库中,让我们来把它转换为布尔值:
150165

151166
<?php
152167

@@ -157,7 +172,7 @@ For example, let's cast the `is_admin` attribute, which is stored in our databas
157172
class User extends Model
158173
{
159174
/**
160-
* The attributes that should be casted to native types.
175+
* 应该被转换成原生类型的属性。
161176
*
162177
* @var array
163178
*/
@@ -166,7 +181,7 @@ For example, let's cast the `is_admin` attribute, which is stored in our databas
166181
];
167182
}
168183

169-
Now the `is_admin` attribute will always be cast to a boolean when you access it, even if the underlying value is stored in the database as an integer:
184+
现在当你访问 `is_admin` 属性时,它将会被转换成布尔值,即便保存在数据库里的值是一个整数:
170185

171186
$user = App\User::find(1);
172187

@@ -175,9 +190,9 @@ Now the `is_admin` attribute will always be cast to a boolean when you access it
175190
}
176191

177192
<a name="array-and-json-casting"></a>
178-
### Array & JSON Casting
193+
### 数组 & JSON 转换
179194

180-
The `array` cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a `JSON` or `TEXT` field type that contains serialized JSON, adding the `array` cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model:
195+
若原本字段保存的是被序列化的 JSON,则 `array` 类型转换将会特别有用。例如,在你的数据库中有一个 `JSON` `TEXT` 字段类型,其包含了 被序列化的 JSON,且对该属性添加了 `array` 类型转换。当你在 Eloquent 模型上访问该属性时,它将会被自动反序列化成一个 PHP 数组:
181196

182197
<?php
183198

@@ -188,7 +203,7 @@ The `array` cast type is particularly useful when working with columns that are
188203
class User extends Model
189204
{
190205
/**
191-
* The attributes that should be casted to native types.
206+
* 应该被转换成原生类型的属性。
192207
*
193208
* @var array
194209
*/
@@ -197,7 +212,7 @@ The `array` cast type is particularly useful when working with columns that are
197212
];
198213
}
199214

200-
Once the cast is defined, you may access the `options` attribute and it will automatically be deserialized from JSON into a PHP array. When you set the value of the `options` attribute, the given array will automatically be serialized back into JSON for storage:
215+
一旦类型转换被定义,则可以访问 `options` 属性,它将会自动把 JSON 反序列化成一个 PHP 数组。当你设置 `options` 属性的值时,指定的数组将会被自动序列化成 JSON 以便进行保存:
201216

202217
$user = App\User::find(1);
203218

0 commit comments

Comments
 (0)