Laravel hasManyThrough

24

我在laravel中对hasManyThrough的概念感到困惑。 我有三个表:

Bookings
    -id (int)
    -some other fields

Meta
    -id (int)
    -booking_id (int)
    -metatype_id (int)
    -some other fields

MetaType
    -id (int)
    -name (string)
    -some other fields

我想要的是一种Eloquent模型,它能够让我拥有一个包含多个MetaType记录的单个预订记录。我曾尝试使用hasManyThrough来解决这个问题,但现在我认为这可能不是最好的方法。

在我的预订模型中,我有:

public function bookingmeta() {
    return $this->hasMany('bookingmeta','booking_id');
}

public function bookingmetatype() {
    return $this->hasManyThrough('bookingmetatype','bookingmeta','booking_id','bookingmetatype_id');
}

但是这种方法无法生成正确的SQL,失败了。我得到了:

select `new_bookingmetatype`.*, `new_bookingmeta`.`booking_id` 
from `new_bookingmetatype` 
inner join `new_bookingmeta` 
on `new_bookingmeta`.`bookingmetatype_id` = `new_bookingmetatype`.`id` 
where `new_bookingmeta`.`booking_id` in (57103)

而我真正想要实现的是

select `new_bookingmetatype`.*, `new_bookingmeta`.`booking_id` 
from `new_bookingmetatype` 
inner join `new_bookingmeta` 
on `new_bookingmeta`.`id` = `new_bookingmetatype`.`bookingmetatype_id`  
where `new_bookingmeta`.`booking_id` in (57103)
如果有人能指点我正确方向,我会非常感激。谢谢。

如果有人能指点我正确方向,我会非常感激。谢谢。

1个回答

50

hasManyThrough 这种方式根本行不通。 它仅适用于像这样的关系:

A hasMany/hasOne B, B hasMany/hasOne C, then A hasManyThrough C (through B)

您这里有一个多对多关系(belongsToMany),其中meta是中间表。

因此,您可以执行以下操作(假设meta是表名,Booking和MetaType是模型):

// Booking model
public function meta()
{
  return $this->belongsToMany('MetaType', 'meta', 'booking_id', 'metatype_id')
        ->withPivot([ ARRAY OF FIELDS YOU NEED FROM meta TABLE ]);
}

然后你就可以访问所有相关的元类型:

$booking->meta; // collection of MetaType models

可以像这样查询(使用急加载):

$booking = Booking::with(['meta' => function ($q) {

  // query related table
  $q->where('someFieldOnMetaTypeTable', 'someValue')

    // and / or pivot table
    ->wherePivot('someFieldOnMetaTable', 'anotherValue');

}])->first();

或在相关表上设置约束条件以过滤预订:

$booking = Booking::whereHas('meta', function ($q) {

  // query related table
  $q->where('someFieldOnMetaTypeTable', 'someValue')

    // and / or pivot table
    ->where('meta.someFieldOnMetaTable', 'anotherValue');

})->first();

注意:只有在使用了预加载关系时,wherePivot 才有效,因此您不能在 whereHas 闭包中使用它。


谢谢,这很有道理 :) - Milan Maharjan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接