【问题标题】:Laravel hasManyThroughLaravel hasManyThrough
【发布时间】:2014-09-19 14:31:12
【问题描述】:

我很难理解 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 类型的 Meta 记录的预订记录。我认为 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)

如果有人能指出我正确的方向,我将不胜感激。谢谢。

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    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 ]);
    }
    

    然后你就可以访问所有关联的 MetaType:

    $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 闭包中使用它。

    【讨论】:

    • 谢谢,这是有道理的:)
    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2014-08-14
    • 2017-12-08
    • 2018-10-18
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多