Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate eloquent-relationships.md (v5.5) #385

Merged

Conversation

Nationalcat
Copy link
Collaborator

Translate eloquent-relationships.md (v5.5)

內容超級超級多....

@Nationalcat Nationalcat mentioned this pull request Nov 16, 2017
73 tasks
@Nationalcat Nationalcat force-pushed the 5.5-eloquent-relationships branch 2 times, most recently from fd03e63 to b303de9 Compare November 26, 2017 16:06
*/
public function user()
{
return $this->belongsTo('App\User');
}
}

In the example above, Eloquent will try to match the `user_id` from the `Phone` model to an `id` on the `User` model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with `_id`. However, if the foreign key on the `Phone` model is not `user_id`, you may pass a custom key name as the second argument to the `belongsTo` method:
在以上範例中,Eloquent 會試著從 `Phone` 模型上的 `user_id` 尋找與在 `User` 模型上相符合的 `id`。Eloquent 透過檢查關聯方法的名稱和使用 `_id` 後綴方法的名稱來決定預設外鍵名稱。然而,如果在 `Phone` 上的外鍵不是 `user_id`,你可以在 `belongsTo` 方法上傳入自訂鍵作為第二個參數:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在以上範例中,Eloquent 會嘗試匹配 Phone 模型的 user_id 至 User 模型的 id

*/
public function user()
{
return $this->belongsTo('App\User', 'foreign_key');
}

If your parent model does not use `id` as its primary key, or you wish to join the child model to a different column, you may pass a third argument to the `belongsTo` method specifying your parent table's custom key:
如果父級模型並沒有使用 `id` 作為它的主鍵,或你希望子模型連結到不同的欄位,你可以在 `belongsTo` 方法上傳入想要的父級資料表的自訂鍵作為第三個參數:
Copy link
Member

@neighborhood999 neighborhood999 Dec 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果你的上層模型不是使用 id 作為主鍵,或是你希望以不同的欄位 join 下層模型,你可以傳遞第三參數至 belongsTo 方法指定層資料表的自訂鍵:


The `belongsTo` relationship allows you to define a default model that will be returned if the given relationship is `null`. This pattern is often referred to as the [Null Object pattern](https://en.wikipedia.org/wiki/Null_Object_pattern) and can help remove conditional checks in your code. In the following example, the `user` relation will return an empty `App\User` model if no `user` is attached to the post:
`belongsTo` 關聯可以讓你定義預設模型,這會因為給定關聯為 `null` 而回傳預設模型。這個模式通常被稱作[無物件模式](https://en.wikipedia.org/wiki/Null_Object_pattern),可以協助移除程式碼中條件檢查。在以下範例中,`user` 關聯會因為沒有 `user` 被附加到 post 上而回傳空的 `App\User` 模型:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果給定的關聯為 nullbelongsTo 關聯可以讓你定義預設模型。這個模式通常被稱作空物件模式,可以協助移除程式碼中條件檢查。


/**
* Get the author of the post.
* 取得該貼文的作者。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

取得該文章的作者。

Copy link
Member

@neighborhood999 neighborhood999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這篇真的很長,辛苦了!


/**
* Get the author of the post.
* 取得該貼文的作者。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

取得該文章的作者。

@@ -146,7 +146,7 @@ To populate the default model with attributes, you may pass an array or Closure
}

/**
* Get the author of the post.
* 取得該貼文的作者。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

取得該文章的作者。


A "one-to-many" relationship is used to define relationships where a single model owns any amount of other models. For example, a blog post may have an infinite number of comments. Like all other Eloquent relationships, one-to-many relationships are defined by placing a function on your Eloquent model:
「一對多」關聯是被用於定義單一模型可以擁有好幾個模型關聯。例如,一篇部落格貼文可以有無數筆回覆。像是所有其他的 Eloquent 關聯,一對多關聯是在你的 Eloquent 模型上放置一個函式來定義關聯:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例如,一篇部落格文章可能有很多評論。

*/
public function comments()
{
return $this->hasMany('App\Comment');
}
}

Remember, Eloquent will automatically determine the proper foreign key column on the `Comment` model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with `_id`. So, for this example, Eloquent will assume the foreign key on the `Comment` model is `post_id`.
請記住,Eloquent 會在 `Comment` 模型上自動決定該屬性外鍵欄位。按照慣例,Eloquent 會使用被關聯的模型「snake case」 名稱與後綴 `_id` 來命名。因此,在這個範例,Eloquent 會假設在 `Comment` 模型上的外鍵是 `post_id`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

請記住 -> 請記得


Once the relationship has been defined, we can access the collection of comments by accessing the `comments` property. Remember, since Eloquent provides "dynamic properties", we can access relationship methods as if they were defined as properties on the model:
關聯一旦被定義,我們能透過存取 `comments` 屬性來存取 comments 的集合。請記住,由於 Eloquent 提供「動態屬性」的關係,我們才能存取模型方法,就像是他們被定義為模型上的屬性:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一旦關聯被定義,我們能透過訪問 comments 屬性來存取 comments 的集合。

*
* @var array
*/
protected $touches = ['post'];

/**
* Get the post that the comment belongs to.
* 取得該回覆所屬的貼文。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回覆 -> 評論
貼文 -> 文章

- [預載入](#eager-loading)
- [限制預載入](#constraining-eager-loads)
- [延遲預載入](#lazy-eager-loading)
- [插入與更新關聯模型](#inserting-and-updating-related-models)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

寫入與更新關聯模型

- [`create` 方法](#the-create-method)
- [更新關聯歸屬](#updating-belongs-to-relationships)
- [更新多對多關聯](#updating-many-to-many-relationships)
- [更新主要關聯的時間戳](#touching-parent-timestamps)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新主要關聯的時間戳記


App\User::find(1)->roles()->save($role, ['expires' => $expires]);

#### Updating A Record On A Pivot Table
#### 修改中介表中的特定紀錄
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

紀錄 -> 記錄


If you need to update an existing row in your pivot table, you may use `updateExistingPivot` method. This method accepts the pivot record foreign key and an array of attributes to update:
如果你需要修改已存在中介表中的紀錄,你可以使用 `updateExistingPivot` 方法。這個方法接受中介表記錄的外鍵和一組要更新的屬性陣列::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

紀錄 -> 記錄

- [定義關聯](#defining-relationships)
- [一對一](#one-to-one)
- [一對多](#one-to-many)
- [一對多(逆向)](#one-to-many-inverse)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一對多(反向)

@neighborhood999 neighborhood999 merged commit f777769 into laravel-taiwan:5.5 Dec 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants