class User extends Eloquent {
public function phone()
{ // 传递给 hasOne 函数的第一个参数是相关模型的名字
return $this->hasOne('Phone');
}
}
$phone = User::find(1)->phone;
这条语句所产生的 SQL 语句如下:
select * from users where id = 1
select * from phones where user_id = 1
注意 Eloquent 假设关系的外键基于模型的名字。在这个例子中假设 Phone
模型使用一个 user_id
外键。
return $this->hasOne('Phone', 'custom_key');
class Phone extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
在上面的例子中,Eloquent 将在 phones 表中寻找 user_id 字段。如果您想定义一个不同的外键字段,您可以通过 belongsTo 函数的第二个参数传递它:
return $this->belongsTo('User', 'custom_key');
class Post extends Eloquent {
public function comments()
{
return $this->hasMany('Comment');
}
}
$comments = Post::find(1)->comments;
$comments = Post::find(1)->comments()->where('title', '=', 'foo')->first();
return $this->hasMany('Comment', 'custom_key');
class Comment extends Eloquent {
public function post()
{
return $this->belongsTo('Post');
}
}
class User extends Eloquent {
public function roles()
{
return $this->belongsToMany('Role');
}
}
注意:需要中间关系表,本例中为 role_user
并且有 user_id
和 role_id
字段。
return $this->belongsToMany('Role', 'user_roles', 'user_id', 'foo_id');
class Role extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
class Photo extends Eloquent {
public function imageable()
{
return $this->morphTo();
}
}
class Staff extends Eloquent {
public function photos()
{
return $this->morphMany('Photo', 'imageable');
}
}
class Order extends Eloquent {
public function photos()
{
return $this->morphMany('Photo', 'imageable');
}
}
$staff = Staff::find(1);
foreach ($staff->photos as $photo) { }
$photo = Photo::find(1);
$imageable = $photo->imageable;
staff
id - integer
name - string
orders
id - integer
price - integer
photos
id - integer
path - string
imageable_id - integer // 所属模型 ID
imageable_type - string // 所属模型
// 获取至少有1条评论的文章
$posts = Post::has('comments')->get();
// 获取至少有3条评论的文章
$posts = Post::has('comments', '>=', 3)->get();
foreach (Book::with('author')->get() as $book)
{
echo $book->author->name;
}
在上面的循环中,只有两个查询被执行:
select * from books
select * from authors where id in (1, 2, 3, 4, 5, ...)
$books = Book::with('author', 'publisher')->get();
$books = Book::with('author.contacts')->get();
$users = User::with(array('posts' => function($query)
{
$query->where('title', 'like', '%first%');
}))->get();
$books = Book::all();
$books->load('author', 'publisher');
// 注意:下面这种实例化时直接赋值的方法,需要开启集体赋值
$comment = new Comment(array('message' => 'A new comment.'));
$post = Post::find(1);
$comment = $post->comments()->save($comment);
在这个例子中,所插入评论的 post_id 字段将自动设置。
为方便理解,上面的第一行相当于:
$comment = new Comment;
$comment->message = 'A new comment.';
// 或者
$comment = with(new Comment)->push(array('message' => 'A new comment.'));
$comment = new Comment(array('message' => 'A new comment.'));
$post = Post::find(1);
$comment = $comment->post()->associate($post)->save();
$user = User::find(1);
$user->roles()->attach(1); // 指定 role_id 为1
同时向中间表插入其它字段的值:
$user->roles()->attach(1, array('describe' => '关系描述'));
$user->roles()->detach(1);
// 只有数组中的 IDs 将会存在关系表中,其它关系将被剔除
$user->roles()->sync(array(1, 2, 3));
同时向中间表插入其它字段的值:
$user->roles()->sync(array(1 => array('describe' => '关系描述')));
$role = new Role(array('name' => 'Editor'));
User::find(1)->roles()->save($role);
同时向中间表插入其它字段的值:
User::find(1)->roles()->save($role, array('describe' => '关系描述'));
class Comment extends Eloquent {
// 子模型中指定更新时需要触发的父模型
protected $touches = array('post');
public function post()
{
return $this->belongsTo('Post');
}
}
$user = User::find(1);
foreach ($user->roles as $role)
{
echo $role->pivot->created_at;
}
需要在定义多对多关系时完成:
return $this->belongsToMany('Role')->withPivot('foo', 'bar');
需要在定义多对多关系时完成:
return $this->belongsToMany('Role')->withTimestamps();