イントロダクションIntroduction
データベーステーブルは大抵の場合他のものと関連しています。たとえばブログポストは多くのコメントを持つか、それを投稿したユーザーと関連しています。Eloquentはそうしたリレーションを簡単に管理し操作できるようにするとともに、様々なタイプのリレーションをサポートしています。Database tables are often related to one another. For example, a blog post may have many comments, or an order could be related to the user who placed it. Eloquent makes managing and working with these relationships easy, and supports several different types of relationships:
- 1対1One To One[#one-to-one]
- 1対多One To Many[#one-to-many]
- 多対多Many To Many[#many-to-many]
- Has Many ThroughHas Many Through[#has-many-through]
- ポリモーフィックリレーションPolymorphic Relations[#polymorphic-relations]
- ポリモーフィック関係の多対多Many To Many Polymorphic Relations[#many-to-many-polymorphic-relations]
リレーションの定義Defining Relationships
EloquentのリレーションとはEloquentモデルクラスの関数として定義します。Eloquentモデル自身と同様にリレーションはパワフルなクエリービルダーとして動作しますので、関数として定義しているリレーションはパワフルなメソッドのチェーンとクエリー能力を提供できるのです。例を見てください。Eloquent relationships are defined as functions on your Eloquent model classes. Since, like Eloquent models themselves, relationships also serve as powerful query builders[/docs/{{version}}/queries], defining relationships as functions provides powerful method chaining and querying capabilities. For example:
$user->posts()->where('active', 1)->get();
リレーションの使い方へ進む前に、リレーションの各タイプをどのように定義するかを学びましょう。But, before diving too deep into using relationships, let's learn how to define each type:
1対1One To One
1対1関係が基本です。たとえばUser
モデルはPhone
モデル一つと関係しているとしましょう。このリレーションを定義するには、phone
メソッドをUser
モデルに世知します。phone
メソッドはベースのEloquentモデルクラスのhasOne
メソッドを結果として返す必要があります。A
one-to-one relationship is a
very basic relation. For
example, a User
model might be associated
with one Phone
.
To define this relationship,
we place a
phone
method on
the User
model.
The phone
method should return the
results of the
hasOne
method
on the base Eloquent model
class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* ユーザーに関連する電話レコードを取得
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
hasOne
メソッドの最初の引数は関係するモデルの名前です。リレーションが定義できたらEloquentの動的プロパティを使って、関係したモデルを取得できます。動的プロパティによりモデル上のプロパティのようにリレーション関数にアクセスできます。The first
argument passed to the
hasOne
method
is the name of the related
model. Once the relationship
is defined, we may retrieve
the related record using
Eloquent's dynamic
properties. Dynamic
properties allow you to
access relationship
functions as if they were
properties defined on the
model:
$phone = User::find(1)->phone;
Eloquentはリレーションの外部キーがモデル名に基づいていると仮定します。この場合自動的にPhone
モデルはuser_id
外部キーを持っていると仮定します。この規約をオーバーライドしたければ、hasOne
メソッドの第2引数を指定してください。Eloquent
assumes the foreign key of
the relationship based on
the model name. In this
case, the Phone
model is automatically
assumed to have a
user_id
foreign
key. If you wish to override
this convention, you may
pass a second argument to
the hasOne
method:
return $this->hasOne('App\Phone', 'foreign_key');
Eloquentは親のid
カラムと一致する外部キーの値を持っていると仮定します。言い換えればEloquentはユーザーのid
カラムの値をPhone
レコードのuser_id
カラムに存在しないか探します。リレーションで他のid
を使いたければ、hadOne
メソッドの第3引数でカスタムキーを指定してください。Additionally,
Eloquent assumes that the
foreign key should have a
value matching the
id
column of
the parent. In other words,
Eloquent will look for the
value of the user's
id
column in
the user_id
column of the
Phone
record.
If you would like the
relationship to use a value
other than id
,
you may pass a third
argument to the
hasOne
method
specifying your custom
key:
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
逆の関係の定義Defining The Inverse Of The Relation
これでUser
からPhone
モデルへアクセスできるようになりました。今度はPhone
モデルからそれを所有しているUser
へアクセスするリレーションを定義しましょう。hasOne
の逆のリレーションを定義するには、belongsTo
メソッドを使います。So, we
can access the
Phone
model
from our User
.
Now, let's define a
relationship on the
Phone
model
that will let us access the
User
that owns
the phone. We can define the
inverse of a
hasOne
relationship using the
belongsTo
method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
/**
* この電話を所有するUserを取得
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
上の例でEloquentはPhone
モデルのuser_id
に一致するid
を持つUser
モデルを見つけようとします。Eloquentはリレーションメソッド名に_id
のサフィックスを付けた名前をデフォルトの外部キー名とします。しかしPhone
モデルの外部キーがuser_id
でなければ、belongsTo
メソッドの第2引数にカスタムキー名を渡してください。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:
/**
* この電話を所有するUserを取得
*/
public function user()
{
return $this->belongsTo('App\User', 'foreign_key');
}
親のモデルの主キーがid
でないか子のモデルと違ったカラムで紐付けたい場合は、親テーブルのカスタムキー名をbelongsTo
メソッドの第3引数に渡してください。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:
/**
* この電話を所有するUserを取得
*/
public function user()
{
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}
1対多One To Many
「1対多」リレーションは一つのモデルが他の多くのモデルを所有する関係を定義するために使います。ブログポストが多くのコメントを持つのが一例です。他のEloquentリレーションと同様に1対多リレーションはEloquentモデルの関数として定義します。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:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* ブログポストのコメントを取得
*/
public function comments()
{
return $this->hasMany('App\Comment');
}
}
EloquentはComment
モデルに対する外部キーを自動的に決めることを心に留めてください。規約によりEloquentは自分自身のモデル名の「スネークケース」に_id
のサフィックスをつけた名前と想定します。ですから今回の例でEloquentはComment
モデルの外部キーをpost_id
であると想定します。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
.
リレーションを定義したらcomments
プロパティによりコメントのコレクションへアクセスできます。Eloquentは「動的プロパティ」を提供しているのでモデルのプロパティとして定義したリレーション関数へアクセスできることを覚えておきましょう。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
functions as if they were
defined as properties on the
model:
$comments = App\Post::find(1)->comments;
foreach ($comments as $comment) {
//
}
もちろん全リレーションはクエリービルダーとしても働きますから、comments
メソッドを呼び出すときにどのコメントを取得するのかという制約を追加でき、クエリーに条件を続けてチェーンでつなげます。Of
course, since all
relationships also serve as
query builders, you can add
further constraints to which
comments are retrieved by
calling the
comments
method
and continuing to chain
conditions onto the
query:
$comments = App\Post::find(1)->comments()->where('title', 'foo')->first();
hasOne
メソッドと同様に、外部キーとローカルキーをhasMany
メソッドに追加の引数として渡すことでオーバーライドできます。Like the
hasOne
method,
you may also override the
foreign and local keys by
passing additional arguments
to the hasMany
method:
return $this->hasMany('App\Comment', 'foreign_key');
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
逆のリレーションを定義Defining The Inverse Of The Relation
これでポストの全コメントにアクセスできます。今度はコメントから親のポストへアクセスできるようにしましょう。hasMany
リレーションの逆を定義するには子のモデルでbelongsTo
メソッドによりリレーション関数を定義します。Now that
we can access all of a
post's comments, let's
define a relationship to
allow a comment to access
its parent post. To define
the inverse of a
hasMany
relationship, define a
relationship function on the
child model which calls the
belongsTo
method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* このコメントを所有するポストを取得
*/
public function post()
{
return $this->belongsTo('App\Post');
}
}
リレーションが定義できたらComment
のPost
モデルをpost
動的プロパティにより取得しましょう。Once the
relationship has been
defined, we can retrieve the
Post
model for
a Comment
by
accessing the
post
"dynamic
property":
$comment = App\Comment::find(1);
echo $comment->post->title;
前例でEloquentはComment
モデルのpost_id
と一致するid
のPost
モデルを見つけようとします。Eloquentはリレーションメソッドの名前に_id
のサフィックスをつけた名前をデフォルトの外部キーとします。しかしComment
モデルの外部キーがpost_id
でなければ、belongsTo
メソッドの第2引数にカスタムキー名を指定してください。In the
example above, Eloquent will
try to match the
post_id
from
the Comment
model to an id
on the Post
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 Comment
model is not
post_id
, you
may pass a custom key name
as the second argument to
the belongsTo
method:
/**
* このコメントを所有するポストを取得
*/
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key');
}
親モデルが主キーにid
を使っていない場合、もしくは子供のモデルに異なったカラムを紐つけたい場合は、belongsTo
メソッドの第3引数にテーブルのカラム名を指定してください。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:
/**
* このコメントを所有するポストを取得
*/
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
多対多Many To Many
多対多の関係はhasOne
とhasMany
リレーションよりも多少複雑な関係です。このような関係として、ユーザ(user)が多くの役目(roles)を持ち、役目(role)も大勢のユーザー(users)に共有されるという例が挙げられます。たとえば多くのユーザーは"管理者"の役目を持っています。users
、roles
、role_user
の3テーブルがこの関係には必要です。role_user
テーブルは関係するモデル名をアルファベット順に並べたもので、user_id
とrole_id
を持つ必要があります。Many-to-many
relations are slightly more
complicated than
hasOne
and
hasMany
relationships. An example of
such a relationship is a
user with many roles, where
the roles are also shared by
other users. For example,
many users may have the role
of "Admin". To
define this relationship,
three database tables are
needed: users
,
roles
, and
role_user
. The
role_user
table
is derived from the
alphabetical order of the
related model names, and
contains the
user_id
and
role_id
columns.
多対多リレーションはベースのEloqunetクラスのbelongsToMany
メソッド呼び出しを記述することで定義します。例としてUser
モデルにroles
メソッドを定義してみましょう。Many-to-many
relationships are defined by
writing a method that calls
the
belongsToMany
method on the base Eloquent
class. For example, let's
define the
roles
method on
our User
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* userに所属する役割を取得
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
リレーションが定義できたら、roles
動的プロパティを使いユーザーの役割にアクセスできます。Once the
relationship is defined, you
may access the user's roles
using the roles
dynamic property:
$user = App\User::find(1);
foreach ($user->roles as $role) {
//
}
もちろん他のリレーションタイプと同様にリレーションを制約するクエリーをroles
に続けてチェーンすることができます。Of
course, like all other
relationship types, you may
call the roles
method to continue chaining
query constraints onto the
relationship:
$roles = App\User::find(1)->roles()->orderBy('name')->get();
前に述べたようにリレーションの結合テーブルの名前を決めるため、Eloquentは2つのモデル名をアルファベット順に結合します。しかしこの規約は自由にオーバーライドできます。belongsToMany
メソッドの第2引数に渡してください。As
mentioned previously, to
determine the table name of
the relationship's joining
table, Eloquent will join
the two related model names
in alphabetical order.
However, you are free to
override this convention.
You may do so by passing a
second argument to the
belongsToMany
method:
return $this->belongsToMany('App\Role', 'user_roles');
結合テーブル名のカスタマイズに加えテーブルのキーカラム名をカスタマイズするには、belongsToMany
メソッドに追加の引数を渡してください。第3引数はリレーションを定義しているモデルの外部キー名で、一方の第4引数には結合するモデルの外部キー名を渡します。In
addition to customizing the
name of the joining table,
you may also customize the
column names of the keys on
the table by passing
additional arguments to the
belongsToMany
method. The third argument
is the foreign key name of
the model on which you are
defining the relationship,
while the fourth argument is
the foreign key name of the
model that you are joining
to:
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
逆のリレーションを定義Defining The Inverse Of The Relationship
多対多のリレーションの逆リレーションを定義するには、関連するモデルでもbelongsToMany
を呼び出してください。引き続きユーザーと役割の例を続けますがRole
モデルでusers
メソッドを定義してみましょう。To define
the inverse of a
many-to-many relationship,
you simply place another
call to
belongsToMany
on your related model. To
continue our user roles
example, let's define the
users
method on
the Role
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
/**
* 役割を所有するユーザー
*/
public function users()
{
return $this->belongsToMany('App\User');
}
}
ご覧の通り一方のUser
と全く同じ定義のリレーションです。違いはApp\User
モデルを参照していことです。同じbelongsToMany
メソッドを使っているのですから、通常のテーブル名、キーカスタマイズのオプションは逆の多対多リレーションを定義するときでも全て使用できます。As you
can see, the relationship is
defined exactly the same as
its User
counterpart, with the
exception of simply
referencing the
App\User
model.
Since we're reusing the
belongsToMany
method, all of the usual
table and key customization
options are available when
defining the inverse of
many-to-many
relationships.
中間テーブルのカラム取得Retrieving Intermediate Table Columns
既に学んだように、多対多リレーションの操作には中間テーブルが必要です。Eloquentこのテーブルを操作する便利な手段を用意しています。例としてUser
オブジェクトが関連するRole
オブジェクトを持っているとしましょう。このリレーションへアクセスした後、モデルのpivot
属性を使い中間テーブルにアクセスできます。As you
have already learned,
working with many-to-many
relations requires the
presence of an intermediate
table. Eloquent provides
some very helpful ways of
interacting with this table.
For example, let's assume
our User
object
has many Role
objects that it is related
to. After accessing this
relationship, we may access
the intermediate table using
the pivot
attribute on the
models:
$user = App\User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
取得したそれぞれのRole
モデルは自動的にpivot
属性と自動的に結合されます。この属性は中間テーブルを表すモデルを含んでおり、他のElouquentモデルと同様に使用できます。Notice
that each Role
model we retrieve is
automatically assigned a
pivot
attribute. This attribute
contains a model
representing the
intermediate table, and may
be used like any other
Eloquent model.
デフォルトでモデルキーはpivot
オブジェクト上のものを表しています。中間テーブルがその他の属性を持っている場合、リレーションを定義するときに指定できます。By
default, only the model keys
will be present on the
pivot
object.
If your pivot table contains
extra attributes, you must
specify them when defining
the relationship:
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
もし中間テーブルのcreated_at
、updated_at
タイムスタンプを自動的に保守したい場合は、withTimestamps
メソッドをリレーション定義に付けてください。If you
want your pivot table to
have automatically
maintained
created_at
and
updated_at
timestamps, use the
withTimestamps
method on the relationship
definition:
return $this->belongsToMany('App\Role')->withTimestamps();
Has Many ThroughHas Many Through
has many
through(〜経由の多対多)リレーションは、仲介するテーブルを通して直接関連付けしていないテーブルへアクセスするための、便利な近道を提供します。たとえばCountry
モデルはUsers
モデルを経由して、多くのPosts
を所有することでしょう。テーブルは以下のような構成になります。The
"has-many-through"
relationship provides a
convenient short-cut for
accessing distant relations
via an intermediate
relation. For example, a
Country
model
might have many
Post
models
through an intermediate
User
model. In
this example, you could
easily gather all blog posts
for a given country. Let's
look at the tables required
to define this
relationship:
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
たとえposts
テーブルにcountry_id
が存在しなくても、hasManyThrough
リレーションではCountryのPostへ$country->posts
によりアクセスできます。このクエリーを行うためにEloquentは仲介するusers
テーブルのcountry_id
を調べます。一致するユーザーIDが存在していたらposts
テーブルのクエリーに利用します。Though
posts
does not
contain a
country_id
column, the
hasManyThrough
relation provides access to
a country's posts via
$country->posts
.
To perform this query,
Eloquent inspects the
country_id
on
the intermediate
users
table.
After finding the matching
user IDs, they are used to
query the posts
table.
ではリレーションのテーブル構造を理解したところで、Country
モデルを定義しましょう。Now that
we have examined the table
structure for the
relationship, let's define
it on the
Country
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
/**
* この国の全ポストを取得
*/
public function posts()
{
return $this->hasManyThrough('App\Post', 'App\User');
}
}
hasManyThrough
メソッドの第一引数は最終的にアクセスしたいモデル名で、第2引数は仲介するモデル名です。The first
argument passed to the
hasManyThrough
method is the name of the
final model we wish to
access, while the second
argument is the name of the
intermediate
model.
リレーションのクエリー実行時では典型的なEloquentの外部キー規約が使用されます。リレーションのキーをカスタマイズしたい場合はhasManyThrough
メソッドの第3引数と、第4引数を指定してください。第3引数は仲介モデルの外部キー名、第4引数は最終的なモデルの外部キー名です。Typical
Eloquent foreign key
conventions will be used
when performing the
relationship's queries. If
you would like to customize
the keys of the
relationship, you may pass
them as the third and fourth
arguments to the
hasManyThrough
method. The third argument
is the name of the foreign
key on the intermediate
model, while the fourth
argument is the name of the
foreign key on the final
model.
class Country extends Model
{
public function posts()
{
return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
}
}
ポリモーフィック関係Polymorphic Relations
テーブル構造Table Structure
ポリモーフィックリレーションはあるモデルを一つの関係だけで、複数のモデルに所属させるものです。たとえばスタッフメンバーと商品の写真を保存するとしましょう。ポリモーフィックリレーションによりphotos
テーブル一つを両方のシナリオで使用できます。最初にこのリレーションを構築するために必要な構造を確認してください。Polymorphic
relations allow a model to
belong to more than one
other model on a single
association. For example,
imagine you want to store
photos for your staff
members and for your
products. Using polymorphic
relationships, you can use a
single photos
table for both of these
scenarios. First, let's
examine the table structure
required to build this
relationship:
staff
id - integer
name - string
products
id - integer
price - integer
photos
id - integer
path - string
imageable_id - integer
imageable_type - string
photos
テーブル上の2つの重要なカラム、imageable_id
とimageable_type
に注目してください。imageable_id
カラムは所有しているスタッフか製品のIDを持っています。一方のimageable_type
カラムは所収されているモデルのクラス名を持っています。imageable_type
はimageable
リレーションにアクセスされた時に返す、所有しているモデルの「タイプ」をORMが決定するためのカラムです。Two
important columns to note
are the
imageable_id
and
imageable_type
columns on the
photos
table.
The
imageable_id
column will contain the ID
value of the owning staff or
product, while the
imageable_type
column will contain the
class name of the owning
model. The
imageable_type
column is how the ORM
determines which
"type" of owning
model to return when
accessing the
imageable
relation.
モデル構造Model Structure
次にこのリレーションを構築するために必要なモデル定義を見てみましょう。Next, let's examine the model definitions needed to build this relationship:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
/**
* 所有している全imageableモデルの取得
*/
public function imageable()
{
return $this->morphTo();
}
}
class Staff extends Model
{
/**
* 全スタッフメンバーの写真の取得
*/
public function photos()
{
return $this->morphMany('App\Photo', 'imageable');
}
}
class Product extends Model
{
/**
* 全商品の写真の取得
*/
public function photos()
{
return $this->morphMany('App\Photo', 'imageable');
}
}
ポリモーフィックリレーションの取得Retrieving Polymorphic Relations
データベーステーブルとモデルが定義できたらモデルを使いリレーションにアクセスできます。たとえばスタッフメンバーの全写真にアクセスするにはphotos
動的プロパティーを使用するだけです。Once your
database table and models
are defined, you may access
the relationships via your
models. For example, to
access all of the photos for
a staff member, we can
simply use the
photos
dynamic
property:
$staff = App\Staff::find(1);
foreach ($staff->photos as $photo) {
//
}
ポリモーフィックモデルからポリモーフィックリレーションの所有者をmorphTo
を呼び出すメソッド名にアクセスすることで取得できます。今回の例の場合Photo
モデルのimageable
メソッドです。動的プロパティーとしてメソッドにアクセスしてみましょう。You may
also retrieve the owner of a
polymorphic relation from
the polymorphic model by
accessing the name of the
method that performs the
call to
morphTo
. In our
case, that is the
imageable
method on the
Photo
model.
So, we will access that
method as a dynamic
property:
$photo = App\Photo::find(1);
$imageable = $photo->imageable;
Photo
モデルのimageable
リレーションは写真を所有しているモデルのタイプにより、Staff
かProduct
インスタンスを返します。The
imageable
relation on the
Photo
model
will return either a
Staff
or
Product
instance, depending on which
type of model owns the
photo.
カスタムポリモーフィックリレーションCustom Polymorphic Types
関連付けられたモデルのタイプを保存するため、デフォルトでLaravelははっきりと区別できるクラス名を使います。たとえば上記の例で、Like
がPost
かComment
に所属しているとすると、likable_type
はデフォルトでApp\Post
かApp\Comment
のどちらかになるでしょう。しかし、データーベースはアプリケーションの内部構造と分離したいかと思います。その場合、リレーションの"morph
map"を定義し、クラス名の代わりに使用する、各モデルに関連づいたテーブル名をEloquentへ指示することができます。By
default, Laravel will use
the fully qualified class
name to store the type of
the related model. For
instance, given the example
above where a
Like
may belong
to a Post
or a
Comment
, the
default
likable_type
would be either
App\Post
or
App\Comment
,
respectively. However, you
may wish to decouple your
database from your
application's internal
structure. In that case, you
may define a relationship
"morph map" to
instruct Eloquent to use the
table name associated with
each model instead of the
class name:
Relation::morphMap([
App\Post::class,
App\Comment::class,
]);
もしくは、各モデルに関連づいた、カスタム文字列を指定することもできます。Or, you may specify a custom string to associate with each model:
Relation::morphMap([
'posts' => App\Post::class,
'likes' => App\Like::class,
]);
morphMap
はAppServiceProvider
の中で登録できます。要望に合わせて、独立したサービスプロバイダを作成することもできるでしょう。You may
register the
morphMap
in
your
AppServiceProvider
or create a separate service
provider if you
wish.
ポリモーフィック関係の多対多Many To Many Polymorphic Relations
テーブル構造Table Structure
伝統的なポリモーフィックリレーションに加え、「多対多」ポリモーフィックリレーションも指定することができます。たとえばブログのPost
とVideo
モデルはTag
モデルに対するポリモーフィックリレーションを共有できます。多対多ポリモーフィックリレーションを使うことで、ブログポストとビデオの両者に所有されている一意のタグのリストを取得できます。最初にテーブル構造を確認しましょう。In
addition to traditional
polymorphic relations, you
may also define
"many-to-many"
polymorphic relations. For
example, a blog
Post
and
Video
model
could share a polymorphic
relation to a
Tag
model.
Using a many-to-many
polymorphic relation allows
you to have a single list of
unique tags that are shared
across blog posts and
videos. First, let's examine
the table
structure:
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
モデル構造Model Structure
次にモデルにその関係を用意しましょう。Post
とVideo
モデルは両方ともベースのEloquentクラスのmorphToMany
メソッドを呼び出すtags
メソッドを持っています。Next,
we're ready to define the
relationships on the model.
The Post
and
Video
models
will both have a
tags
method
that calls the
morphToMany
method on the base Eloquent
class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* ポストに対する全タグを取得
*/
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
}
逆のリレーションの定義Defining The Inverse Of The Relationship
次にTag
モデルで関係する各モデルに対するメソッドを定義する必要があります。ですからこの例の場合、posts
メソッドとvideos
メソッドを用意します。Next, on
the Tag
model,
you should define a method
for each of its related
models. So, for this
example, we will define a
posts
method
and a videos
method:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
/**
* このタグをつけた全ポストの取得
*/
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
/**
* このタグをつけた全ビデオの取得
*/
public function videos()
{
return $this->morphedByMany('App\Video', 'taggable');
}
}
リレーションの取得Retrieving The Relationship
データベーステーブルとモデルが定義できたら、モデルを使いリレーションにアクセスできます。たとえばポストに対する全タグへアクセスするには、単にtags
動的プロパティーを使用するだけです。Once your
database table and models
are defined, you may access
the relationships via your
models. For example, to
access all of the tags for a
post, you can simply use the
tags
dynamic
property:
$post = App\Post::find(1);
foreach ($post->tags as $tag) {
//
}
さらにmorphedByMany
を呼び出すメソッドの名前にアクセスし、ポリモーフィックモデルからポリモーフィックリレーションの所有者を取得することも可能です。この例の場合Tag
モデルのposts
とvideos
メソッドです。では動的プロパティーとしてメソッドを呼び出しましょう。You may
also retrieve the owner of a
polymorphic relation from
the polymorphic model by
accessing the name of the
method that performs the
call to
morphedByMany
.
In our case, that is the
posts
or
videos
methods
on the Tag
model. So, you will access
those methods as dynamic
properties:
$tag = App\Tag::find(1);
foreach ($tag->videos as $video) {
//
}
リレーションのクエリーQuerying Relations
Eloquentリレーションは全てメソッドとして定義され、リレーションのクエリーを実際に記述しなくてもメソッドを呼び出すことでそのリレーションのインスタンスを取得できます。付け加えるなら、全タイプのEloquentリレーションもクエリービルダーとしても動作し、データベースに対してSQLが最終的に実行される前にそのリレーションのクエリーをチェーンで続けて記述できます。Since all types of Eloquent relationships are defined via functions, you may call those functions to obtain an instance of the relationship without actually executing the relationship queries. In addition, all types of Eloquent relationships also serve as query builders[/docs/{{version}}/queries], allowing you to continue to chain constraints onto the relationship query before finally executing the SQL against your database.
たとえばブログシステムで関連した多くのPost
モデルを持つUser
モデルを想像してください。For
example, imagine a blog
system in which a
User
model has
many associated
Post
models:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* ユーザーの全ポストの取得
*/
public function posts()
{
return $this->hasMany('App\Post');
}
}
次のようにposts
リレーションのクエリーに追加の制約を付け加えられます。You may
query the posts
relationship and add
additional constraints to
the relationship like
so:
$user = App\User::find(1);
$user->posts()->where('active', 1)->get();
リレーションにもクエリービルダーメソッドが使用できる点に注目です!Note that you are able to use any of the query builder[/docs/{{version}}/queries] methods on the relationship!
リレーションメソッド v.s. 動的プロパティーRelationship Methods Vs. Dynamic Properties
Eloquentリレーションクエリーに追加の制約を加える必要がなければ、シンプルにそのリレーションへプロパティーとしてアクセスできます。User
とPost
の例を続けるとして、ユーザーの全ポストには次のようにアクセスできます。If you do
not need to add additional
constraints to an Eloquent
relationship query, you may
simply access the
relationship as if it were a
property. For example,
continuing to use our
User
and
Post
example
models, we may access all of
a user's posts like
so:
$user = App\User::find(1);
foreach ($user->posts as $post) {
//
}
動的プロパティーは「遅延ロード」されます。つまり実際にアクセスされた時にだけそのリレーションのデータはロードされます。そのため開発者は多くの場合にEagerローディングを使い、モデルをロードした後にアクセスするリレーションを前もってロードしておきます。Eagerロードはモデルのリレーションをロードするため実行されるSQLクエリーを大幅に減らしてくれます。Dynamic properties are "lazy loading", meaning they will only load their relationship data when you actually access them. Because of this, developers often use eager loading[#eager-loading] to pre-load relationships they know will be accessed after loading the model. Eager loading provides a significant reduction in SQL queries that must be executed to load a model's relations.
存在するリレーションのクエリーQuerying Relationship Existence
関連付けたモデルのレコードに基づいて、モデルのレコードに対するマッチングを絞り込みたい場合もあるでしょう。たとえば最低でも一つのコメントを持つ、全ブログポストを取得したい場合を考えてください。これを行うためにはhas
メソッドを使用します。When
accessing the records for a
model, you may wish to limit
your results based on the
existence of a relationship.
For example, imagine you
want to retrieve all blog
posts that have at least one
comment. To do so, you may
pass the name of the
relationship to the
has
method:
// 最低1つのコメントを持つ全ポストの取得…
$posts = App\Post::has('comments')->get();
演算子と数を指定しクエリーをカスタマイズすることもできます。You may also specify an operator and count to further customize the query:
// 3つ以上のコメントを持つ全ポストの取得…
$posts = Post::has('comments', '>=', 3)->get();
ネストしたhas
文は「ドット」記法で組立てられます。たとえば最低一つのコメントと評価を持つ全ポストを取得する場合です。Nested
has
statements
may also be constructed
using "dot"
notation. For example, you
may retrieve all posts that
have at least one comment
and vote:
// 最低1つのコメントと、それに対する評価を持つ全ポストの取得…
$posts = Post::has('comments.votes')->get();
もっと強力な機能がお望みならばhas
の問い合わせに"where"で条件をつけるために、whereHas
やorWhereHas
を利用して下さい。これらのメソッドによりリレーションの制約にカスタマイズした制約を追加できます。たとえばコメントの内容を調べることです。If you
need even more power, you
may use the
whereHas
and
orWhereHas
methods to put
"where" conditions
on your has
queries. These methods allow
you to add customized
constraints to a
relationship constraint,
such as checking the content
of a comment:
// like foo%の制約に一致する最低1つのコメントを持つ全ポストの取得
$posts = Post::whereHas('comments', function ($query) {
$query->where('content', 'like', 'foo%');
})->get();
EagerローディングEager Loading
Eloquentリレーションをプロパティーとしてアクセする場合、リレーションのデータは「遅延ロード」されます。つまりプロパティーにアクセスされるまで実際にリレーションのデータはロードされることはありません。しかし、Eloquentでは親モデルにクエリーする時点で「Eagerロード」できます。EagerローディングはN+1クエリー問題を軽減するために用意しています。たとえばBook
モデルがAuthor
モデルと関連していると考えてください。関係はこのように定義されます。When
accessing Eloquent
relationships as properties,
the relationship data is
"lazy loaded".
This means the relationship
data is not actually loaded
until you first access the
property. However, Eloquent
can "eager load"
relationships at the time
you query the parent model.
Eager loading alleviates the
N 1 query problem. To
illustrate the N 1 query
problem, consider a
Book
model that
is related to
Author
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
/**
* この本を書いた著者を取得
*/
public function author()
{
return $this->belongsTo('App\Author');
}
}
では全書籍とその著者を取得しましょう。Now, let's retrieve all books and their authors:
$books = App\Book::all();
foreach ($books as $book) {
echo $book->author->name;
}
このループではまず全ての本をテーブルから取得するために1クエリー実行され、それから著者をそれぞれの本について取得します。ですから25冊あるならば、このループで26クエリーが発生します。This loop will execute 1 query to retrieve all of the books on the table, then another query for each book to retrieve the author. So, if we have 25 books, this loop would run 26 queries: 1 for the original book, and 25 additional queries to retrieve the author of each book.
ありがたいことにクエリーの数を徹底的に減らすためにEagerローディングを使うことができます。with
メソッドを使い指定してください。Thankfully,
we can use eager loading to
reduce this operation to
just 2 queries. When
querying, you may specify
which relationships should
be eager loaded using the
with
method:
$books = App\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;
}
この操作では2つだけしかクエリーが実行されません。For this operation, only two queries will be executed:
select * from books
select * from authors where id in (1, 2, 3, 4, 5, ...)
複数のリレーションに対するEagerローディングEager Loading Multiple Relationships
一回の操作で異なった複数のリレーションをEagerローディングする必要がある場合もあります。その場合でも、ただwith
メソッドに引数を追加で渡すだけです。Sometimes
you may need to eager load
several different
relationships in a single
operation. To do so, just
pass additional arguments to
the with
method:
$books = App\Book::with('author', 'publisher')->get();
ネストしたEagerローディングNested Eager Loading
ネストしたリレーションをEagerロードする場合は「ドット」記法が使用できます。例としてEloquent文で全著者と著者個人のコンタクトも全部Eagerロードしてみましょう。To eager load nested relationships, you may use "dot" syntax. For example, let's eager load all of the book's authors and all of the author's personal contacts in one Eloquent statement:
$books = App\Book::with('author.contacts')->get();
Eagerロードの制約Constraining Eager Loads
場合によりリレーションをEagerロードしたいが、Eagerロードクエリーに制約を追加したい場合があります。例を見てください。Sometimes you may wish to eager load a relationship, but also specify additional query constraints for the eager loading query. Here's an example:
$users = App\User::with(['posts' => function ($query) {
$query->where('title', 'like', '%first%');
}])->get();
この例でEloquentはtitle
カラムの内容にfirst
という言葉を含むポストのみをEagerロードしています。もちろんEagerローディング操作をもっとカスタマイズするために他のクエリービルダーを呼び出すこともできます。In this
example, Eloquent will only
eager load posts that if the
post's title
column contains the word
first
. Of
course, you may call other
query
builder[/docs/{{version}}/queries]
to further customize the
eager loading
operation:
$users = App\User::with(['posts' => function ($query) {
$query->orderBy('created_at', 'desc');
}])->get();
遅延EagerローディングLazy Eager Loading
既に親のモデルを取得した後にリレーションをEagerロードする必要がある場合もあるでしょう。たとえばこれはどの関連しているモデルをロードするかを動的に決める場合に便利です。Sometimes you may need to eager load a relationship after the parent model has already been retrieved. For example, this may be useful if you need to dynamically decide whether to load related models:
$books = App\Book::all();
if ($someCondition) {
$books->load('author', 'publisher');
}
Eagerロードに追加の制約を付ける必要があるなら、load
メソッドへ「クロージャー」を渡してください。If you
need to set additional query
constraints on the eager
loading query, you may pass
a Closure
to
the load
method:
$books->load(['author' => function ($query) {
$query->orderBy('published_date', 'asc');
}]);
関係したモデルの挿入Inserting Related Models
saveメソッドThe Save Method
Eloquentは新しいモデルをリレーションに追加するために便利なメソッドを用意しています。たとえばPost
モデルに新しいComment
を挿入する必要がある場合です。Comment
のpost_id
属性を自分で設定する代わりに、リレーションのsave
メソッドで直接Comment
を挿入できます。Eloquent
provides convenient methods
for adding new models to
relationships. For example,
perhaps you need to insert a
new Comment
for
a Post
model.
Instead of manually setting
the post_id
attribute on the
Comment
, you
may insert the
Comment
directly from the
relationship's
save
method:
$comment = new App\Comment(['message' => 'A new comment.']);
$post = App\Post::find(1);
$post->comments()->save($comment);
動的プロパティーとしてcomments
リレーションにアクセスできない点には注意してください。代わりにリレーションの取得でcomments
メソッドを呼び出しています。save
メソッドは自動的に新しいComment
モデルのpost_id
へ適した値を代入します。Notice
that we did not access the
comments
relationship as a dynamic
property. Instead, we called
the comments
method to obtain an instance
of the relationship. The
save
method
will automatically add the
appropriate
post_id
value
to the new
Comment
model.
複数の関連したモデルを保存する必要があるなら、saveMany
メソッドを使用できます。If you
need to save multiple
related models, you may use
the saveMany
method:
$post = App\Post::find(1);
$post->comments()->saveMany([
new App\Comment(['message' => 'A new comment.']),
new App\Comment(['message' => 'Another comment.']),
]);
saveと多対多関係の挿入Save & Many To Many Relationships
多対多リレーションを操作する場合、save
メソッドの第2引数へ追加の中間テーブルの属性を指定できます。When
working with a many-to-many
relationship, the
save
method
accepts an array of
additional intermediate
table attributes as its
second argument:
App\User::find(1)->roles()->save($role, ['expires' => $expires]);
createメソッドThe Create Method
save
とsaveMany
メソッドに加え、create
メソッドも使用できます。属性の配列を引数に受け付け、モデルを作成しデータベースへ挿入します。save
とcreate
の違いはsave
が完全なEloquentモデルを受け付けるのに対し、create
は普通のPHPの「配列」を受け付ける点です。In
addition to the
save
and
saveMany
methods, you may also use
the create
method, which accepts an
array of attributes, creates
a model, and inserts it into
the database. Again, the
difference between
save
and
create
is that
save
accepts a
full Eloquent model instance
while create
accepts a plain PHP
array
:
$post = App\Post::find(1);
$comment = $post->comments()->create([
'message' => '新しいコメント。',
]);
create
メソッドを使用する前に属性の複数代入に関するドキュメントを読んでおいてください。Before
using the
create
method,
be sure to review the
documentation on attribute
mass
assignment[/docs/{{version}}/eloquent#mass-assignment].
"Belongs To"リレーションの更新Updating "Belongs To" Relationships
belongsTo
リレーションを更新する場合はassociate
メソッドを使います。このメソッドは子モデルへ外部キーをセットします。When
updating a
belongsTo
relationship, you may use
the associate
method. This method will set
the foreign key on the child
model:
$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();
belongsTo
リレーションを削除する場合はdissociate
メソッドを使用します。このメソッドはリレーションの子モデルの外部キーをリセットします。When
removing a
belongsTo
relationship, you may use
the dissociate
method. This method will
reset the foreign key as
well as the relation on the
child model:
$user->account()->dissociate();
$user->save();
多対多関係の挿入Many To Many Relationships
attach/detachAttaching / Detaching
多対多リレーションを操作時により便利なように、Eloquentはヘルパメソッドをいくつか用意しています。例としてユーザーが多くの役割りを持ち、役割も多くのユーザーを持てる場合を考えてみましょう。モデルを結びつけている中間テーブルにレコードを挿入することにより、ユーザーに役割を持たせるにはattach
メソッドを使います。When
working with many-to-many
relationships, Eloquent
provides a few additional
helper methods to make
working with related models
more convenient. For
example, let's imagine a
user can have many roles and
a role can have many users.
To attach a role to a user
by inserting a record in the
intermediate table that
joins the models, use the
attach
method:
$user = App\User::find(1);
$user->roles()->attach($roleId);
モデルにリレーションを割りつけるときに中間テーブルに挿入したい追加のデータを配列で渡すこともできます。When attaching a relationship to a model, you may also pass an array of additional data to be inserted into the intermediate table:
$user->roles()->attach($roleId, ['expires' => $expires]);
もちろんユーザーから役割を削除する必要がある場合もあるでしょう。多対多リレーションのレコードを削除するにはdetach
メソッドを使います。detach
メソッドは中間テーブルから対応するレコードを削除します。しかし両方のモデルはデータベースに残ります。Of
course, sometimes it may be
necessary to remove a role
from a user. To remove a
many-to-many relationship
record, use the
detach
method.
The detach
method will remove the
appropriate record out of
the intermediate table;
however, both models will
remain in the
database:
// ユーザーから役割を一つ切り離す…
$user->roles()->detach($roleId);
// ユーザーから役割を全部切り離す…
$user->roles()->detach();
便利なようにattach
とdetach
の両方共にIDの配列を指定することができます。For
convenience,
attach
and
detach
also
accept arrays of IDs as
input:
$user = App\User::find(1);
$user->roles()->detach([1, 2, 3]);
$user->roles()->attach([1 => ['expires' => $expires], 2, 3]);
便利なsyncSyncing For Convenience
多対多の関連を構築するためにsync
メソッドも使用できます。sync
メソッドへは中間テーブルに設置しておくIDの配列を渡します。その配列に指定されなかったIDは中間テーブルから削除されます。ですからこの操作が完了すると、中間テーブルには配列中のIDだけが存在することになります。You may
also use the
sync
method to
construct many-to-many
associations. The
sync
method
accepts an array of IDs to
place on the intermediate
table. Any IDs that are not
in the given array will be
removed from the
intermediate table. So,
after this operation is
complete, only the IDs in
the array will exist in the
intermediate
table:
$user->roles()->sync([1, 2, 3]);
IDと一緒に中間テーブルの追加の値を渡すことができます。You may also pass additional intermediate table values with the IDs:
$user->roles()->sync([1 => ['expires' => true], 2, 3]);
親のタイムスタンプの更新Touching Parent Timestamps
Comment
がPost
に所属しているように、あるモデルが他のモデルに所属(belongsTo
もしくはbelongsToMany
)しており、子のモデルが更新される時に親のタイムスタンプを更新できると便利なことがあります。たとえばComment
モデルが更新されたら、所有者のPost
のupdated_at
タイムスタンプを自動的に"touch"したい場合です。Eloquentなら簡単です。子のモデルにtouches
プロパティを追加しリレーション名を指定してください。When a
model belongsTo
or
belongsToMany
another model, such as a
Comment
which
belongs to a
Post
, it is
sometimes helpful to update
the parent's timestamp when
the child model is updated.
For example, when a
Comment
model
is updated, you may want to
automatically
"touch" the
updated_at
timestamp of the owning
Post
. Eloquent
makes it easy. Just add a
touches
property containing the
names of the relationships
to the child
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* 更新する全リレーション
*
* @var array
*/
protected $touches = ['post'];
/**
* ポストに所属しているコメント取得
*/
public function post()
{
return $this->belongsTo('App\Post');
}
}
これでComment
が更新されると、所有しているPost
のupdated_at
カラムも同時に更新されます。Now, when
you update a
Comment
, the
owning Post
will have its
updated_at
column updated as
well:
$comment = App\Comment::find(1);
$comment->text = 'このコメントを修正!';
$comment->save();