Skip to content

Commit

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

* 12/7 修正部分翻譯問題
  • Loading branch information
Nationalcat authored and neighborhood999 committed Dec 8, 2017
1 parent 7eab1d7 commit aa6979b
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions eloquent-serialization.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
# Eloquent: Serialization
# Eloquent:序列化

- [Introduction](#introduction)
- [Serializing Models & Collections](#serializing-models-and-collections)
- [Serializing To Arrays](#serializing-to-arrays)
- [Serializing To JSON](#serializing-to-json)
- [Hiding Attributes From JSON](#hiding-attributes-from-json)
- [Appending Values To JSON](#appending-values-to-json)
- [Date Serialization](#date-serialization)
- [介紹](#introduction)
- [序列化模型與集合](#serializing-models-and-collections)
- [序列化成陣列](#serializing-to-arrays)
- [序列化成 JSON](#serializing-to-json)
- [JSON 隱藏屬性](#hiding-attributes-from-json)
- [將值附加到 JSON](#appending-values-to-json)
- [日期序列化](#date-serialization)

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

When building JSON APIs, you will often need to convert your models and relationships to arrays or JSON. Eloquent includes convenient methods for making these conversions, as well as controlling which attributes are included in your serializations.
建構 JSON API 時,你經常會需要將模型和關聯轉換成陣列或 JSONEloquent 已經包含了這些用來轉換的方便方法,以及控制序列化中應該包含哪些屬性。

<a name="serializing-models-and-collections"></a>
## Serializing Models & Collections
## 序列化模型與集合

<a name="serializing-to-arrays"></a>
### Serializing To Arrays
### 序列化成陣列

To convert a model and its loaded [relationships](/docs/{{version}}/eloquent-relationships) to an array, you should use the `toArray` method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays:
要將模型以及被一同載入的[關聯](/docs/{{version}}/eloquent-relationships)轉換成陣列,你應該使用 `toArray` 方法。這個方法會採用遞迴的方式,因此,所有屬性和關聯(包含關聯中的關聯)都會被轉換成陣列:

$user = App\User::with('roles')->first();

return $user->toArray();

You may also convert entire [collections](/docs/{{version}}/eloquent-collections) of models to arrays:
你也可以將整個模型[集合](/docs/{{version}}/eloquent-collections)轉換成陣列:

$users = App\User::all();

return $users->toArray();

<a name="serializing-to-json"></a>
### Serializing To JSON
### 序列化成 JSON

To convert a model to JSON, you should use the `toJson` method. Like `toArray`, the `toJson` method is recursive, so all attributes and relations will be converted to JSON:
你應該使用 `toJson` 方法來將模型轉換成 JSON。像是 `toArray``toJson` 方法會採用遞迴的方式,因此,所有屬性和關聯都會被轉換成 JSON

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

return $user->toJson();

Alternatively, you may cast a model or collection to a string, which will automatically call the `toJson` method on the model or collection:
或者,你可以將模型或關聯轉換成字串,這會自動在模型或集合上呼叫 `toJson` 方法。

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

return (string) $user;

Since models and collections are converted to JSON when cast to a string, you can return Eloquent objects directly from your application's routes or controllers:
轉換成字串時,由於模型與合集被轉換成 JSON,你能直接從應用程式的路由或控制器中回傳 Eloquent 物件:

Route::get('users', function () {
return App\User::all();
});

<a name="hiding-attributes-from-json"></a>
## Hiding Attributes From JSON
## JSON 隱藏屬性

Sometimes you may wish to limit the attributes, such as passwords, that are included in your model's array or JSON representation. To do so, add a `$hidden` property to your model:
有時你可能希望限制包含在模型的陣列或 JSON 的屬性,像是密碼。可以新增 `$hidden` 屬性到你的模型來達到這個目的:

<?php

Expand All @@ -66,16 +66,16 @@ Sometimes you may wish to limit the attributes, such as passwords, that are incl
class User extends Model
{
/**
* The attributes that should be hidden for arrays.
* 應該隱藏陣列中的屬性。
*
* @var array
*/
protected $hidden = ['password'];
}

> {note} When hiding relationships, use the relationship's method name.
> {note} 當隱藏關聯時,使用關聯方法名稱。
Alternatively, you may use the `visible` property to define a white-list of attributes that should be included in your model's array and JSON representation. All other attributes will be hidden when the model is converted to an array or JSON:
或者,你可以使用 `visible` 屬性來定義應該被包含在模型的陣列或 JSON 的屬性的白名單。當模型被轉換成陣列或 JSON 時,所有未包含的屬性將會被隱藏:

<?php

Expand All @@ -86,27 +86,27 @@ Alternatively, you may use the `visible` property to define a white-list of attr
class User extends Model
{
/**
* The attributes that should be visible in arrays.
* 應該顯示陣列中的屬性。
*
* @var array
*/
protected $visible = ['first_name', 'last_name'];
}

#### Temporarily Modifying Attribute Visibility
#### 暫時修改屬性的可見性

If you would like to make some typically hidden attributes visible on a given model instance, you may use the `makeVisible` method. The `makeVisible` method returns the model instance for convenient method chaining:
如果你想要在給定模型實例上顯示原先被隱藏的屬性,你可以使用 `makeVisible` 方法。`makeVisible` 方法會回傳模型實例,讓你可以方便鏈結方法:

return $user->makeVisible('attribute')->toArray();

Likewise, if you would like to make some typically visible attributes hidden on a given model instance, you may use the `makeHidden` method.
同樣的,如果你想要在給定模型實例上隱藏原先被顯示的屬性,你可以使用 `makeHidden` 方法。

return $user->makeHidden('attribute')->toArray();

<a name="appending-values-to-json"></a>
## Appending Values To JSON
## 將值附加到 JSON

Occasionally, when casting models to an array or JSON, you may wish to add attributes that do not have a corresponding column in your database. To do so, first define an [accessor](/docs/{{version}}/eloquent-mutators) for the value:
有時候,將模型轉換為陣列或JSON 時,你可能希望在資料庫中新增沒有相應欄位的屬性。要達到此目的,首先為這個值定義[存取器](/docs/{{version}}/eloquent-mutators)

<?php

Expand All @@ -117,7 +117,7 @@ Occasionally, when casting models to an array or JSON, you may wish to add attri
class User extends Model
{
/**
* Get the administrator flag for the user.
* 為使用者取得管理者的標誌。
*
* @return bool
*/
Expand All @@ -127,7 +127,7 @@ Occasionally, when casting models to an array or JSON, you may wish to add attri
}
}

After creating the accessor, add the attribute name to the `appends` property on the model. Note that attribute names are typically referenced in "snake case", even though the accessor is defined using "camel case":
建立存取器後,在該模型上新增屬性名稱到 `appends` 屬性。請注意,屬性名稱通常採用「snake case」,甚至使用「駝峰式命名」來定義存取器:

<?php

Expand All @@ -138,19 +138,19 @@ After creating the accessor, add the attribute name to the `appends` property on
class User extends Model
{
/**
* The accessors to append to the model's array form.
* 該存取器被附加到模型的陣列表單。
*
* @var array
*/
protected $appends = ['is_admin'];
}

Once the attribute has been added to the `appends` list, it will be included in both the model's array and JSON representations. Attributes in the `appends` array will also respect the `visible` and `hidden` settings configured on the model.
該屬性一旦被新增到 `appends` 清單,它會被包含在模型的陣列與 JSON 中。`appends` 陣列中的屬性也會依循模型上的 `visible` `hidden` 設定。

<a name="date-serialization"></a>
## Date Serialization
## 日期序列化

Laravel extends the [Carbon](https://github.com/briannesbitt/Carbon) date library in order to provide convenient customization of Carbon's JSON serialization format. To customize how all Carbon dates throughout your application are serialized, use the `Carbon::serializeUsing` method. The `serializeUsing` method accepts a Closure which returns a string representation of the date for JSON serialization:
Laravel 擴充了 [Carbon](https://github.com/briannesbitt/Carbon) 日期函式庫,這會提供方便定製 CarbonJSON 序列化格式。要如何為你所有的應用程式序列化定製的 Carbon 日期,請使用 `Carbon::serializeUsing` 方法。`serializeUsing` 方法接受一個閉包,閉包會是為 JSON 序列化所回傳的字串型別日期:

<?php

Expand All @@ -162,7 +162,7 @@ Laravel extends the [Carbon](https://github.com/briannesbitt/Carbon) date librar
class AppServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
* 執行註冊後,啟動服務。
*
* @return void
*/
Expand All @@ -174,7 +174,7 @@ Laravel extends the [Carbon](https://github.com/briannesbitt/Carbon) date librar
}

/**
* Register bindings in the container.
* 在容器中註冊綁定。
*
* @return void
*/
Expand Down

0 comments on commit aa6979b

Please sign in to comment.