You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: eloquent-collections.md
+21-16Lines changed: 21 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,23 @@
1
-
# Laravel 的 Eloquent 集合
1
+
# Eloquent: 集合
2
2
3
-
-[Introduction](#introduction)
4
-
-[Available Methods](#available-methods)
5
-
-[Custom Collections](#custom-collections)
3
+
-[简介](#introduction)
4
+
-[可用的方法](#available-methods)
5
+
-[自定义集合](#custom-collections)
6
6
7
7
<aname="introduction"></a>
8
-
## Introduction
8
+
## 简介
9
9
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.
Of course, all collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:
12
+
当然,所有集合都可以作为迭代器,来让你像遍历一个 PHP 数组一样来遍历一个集合:
13
13
14
14
$users = App\User::where('active', 1)->get();
15
15
16
16
foreach ($users as $user) {
17
17
echo $user->name;
18
18
}
19
19
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:
@@ -28,14 +28,17 @@ However, collections are much more powerful than arrays and expose a variety of
28
28
return $user->name;
29
29
});
30
30
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.
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:
@@ -143,4 +147,5 @@ If you need to use a custom `Collection` object with your own extension methods,
143
147
}
144
148
}
145
149
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.
Copy file name to clipboardExpand all lines: eloquent-mutators.md
+53-38Lines changed: 53 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,27 @@
1
-
# Laravel 的 Eloquent 修改器
1
+
# Eloquent: 修改器
2
2
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)
10
10
11
11
<aname="introduction"></a>
12
-
## Introduction
12
+
## 简介
13
13
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.
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).
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:
@@ -32,7 +32,7 @@ To define an accessor, create a `getFooAttribute` method on your model where `Fo
32
32
class User extends Model
33
33
{
34
34
/**
35
-
* Get the user's first name.
35
+
* 获取用户的名字。
36
36
*
37
37
* @param string $value
38
38
* @return string
@@ -43,16 +43,16 @@ To define an accessor, create a `getFooAttribute` method on your model where `Fo
43
43
}
44
44
}
45
45
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:
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:
@@ -63,7 +63,7 @@ To define a mutator, define a `setFooAttribute` method on your model where `Foo`
63
63
class User extends Model
64
64
{
65
65
/**
66
-
* Set the user's first name.
66
+
* 设定用户的名字。
67
67
*
68
68
* @param string $value
69
69
* @return void
@@ -74,18 +74,20 @@ To define a mutator, define a `setFooAttribute` method on your model where `Foo`
74
74
}
75
75
}
76
76
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`:
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.
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:
@@ -96,7 +98,7 @@ By default, Eloquent will convert the `created_at` and `updated_at` columns to i
96
98
class User extends Model
97
99
{
98
100
/**
99
-
* The attributes that should be mutated to dates.
101
+
* 应被转换为日期的属性。
100
102
*
101
103
* @var array
102
104
*/
@@ -107,23 +109,23 @@ By default, Eloquent will convert the `created_at` and `updated_at` columns to i
107
109
];
108
110
}
109
111
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:
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:
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:
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
148
163
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:
@@ -157,7 +172,7 @@ For example, let's cast the `is_admin` attribute, which is stored in our databas
157
172
class User extends Model
158
173
{
159
174
/**
160
-
* The attributes that should be casted to native types.
175
+
* 应该被转换成原生类型的属性。
161
176
*
162
177
* @var array
163
178
*/
@@ -166,7 +181,7 @@ For example, let's cast the `is_admin` attribute, which is stored in our databas
166
181
];
167
182
}
168
183
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`属性时,它将会被转换成布尔值,即便保存在数据库里的值是一个整数:
170
185
171
186
$user = App\User::find(1);
172
187
@@ -175,9 +190,9 @@ Now the `is_admin` attribute will always be cast to a boolean when you access it
175
190
}
176
191
177
192
<aname="array-and-json-casting"></a>
178
-
### Array & JSON Casting
193
+
### 数组 & JSON 转换
179
194
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:
@@ -188,7 +203,7 @@ The `array` cast type is particularly useful when working with columns that are
188
203
class User extends Model
189
204
{
190
205
/**
191
-
* The attributes that should be casted to native types.
206
+
* 应该被转换成原生类型的属性。
192
207
*
193
208
* @var array
194
209
*/
@@ -197,7 +212,7 @@ The `array` cast type is particularly useful when working with columns that are
197
212
];
198
213
}
199
214
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:
0 commit comments