Laravel表关系到中间表的关系

19

我不知道如何在Laravel中设置我的模型,该模型与一个连接到pivot表的表相关联。

问题如下:

假设我有

Locations
    id
    name

area_types
    id
    name

area_type_location (Pivot table)
    id
    location_id
    area_type_id

area_test
    id
    area_type_location_id
    clean
    headCount

表格之间的关系是不同区域类型属于不同位置。

例如:沙滩,25米游泳池,儿童游泳池,烧烤等

区域测试与旋转表相连,因为测试必须从现有区域生成,在这种情况下,它是在不同位置注册的区域。因此,它必须进行每日测试、测量等。

我了解区域类型和位置之间的结构,用于多对多关系,但我不能理解如何构建我的area_test模型?我怎样从locations表中获取数据 - 我的测试在哪里?

我应该为我的旋转表创建一个模型吗?在Laravel中,这是一种好实践方法吗?

是否有人有相同的用例?

我阅读了有关Eloquent关系的文档has many through,但我了解到它并没有提及如何通过旋转表。我不太清楚我的用例是否相同。

谢谢


“area_id”在“area_type_location”表中的外键关联哪个表? - Nikola Kirincic
抱歉,之前匆忙回复了。现在编辑了问题,明确指出与“area_types”表相关。 - Robert Tirta
2个回答

16
最后,显然有几种方法可以从locations表中获取数据到area_tests
在tinker中尝试过,它有效。

第一选项

我需要为我的数据透视表创建一个数据透视模型:

class LocationAreaType extends Pivot{

    public function location(){
        return $this->belongsTo(Location::class);
    }

    public function areaType(){
        return $this->belongsTo(AreaType::class);
    }

    public function AreaTests(){
        return $this->hasMany(AreaTest::class, 'area_type_location_id');
    }

}

我可以在我的位置表中创建我需要的hasManyThrough关系。
public function areaTests()
{
    return $this->hasManyThrough(
        AreaTest::class,
        LocationAreaType::class,
        'location_id',
        'area_type_location_id'
    );
}

这样我就可以通过$location->areaTests轻松获取到areaTests。我的问题不是确定area_type_location_id作为外键。你需要确定这一点,显然当我扩展pivot并使用hasMany时,laravel不能自动识别外键。

第二个选项

另一种访问方法是通过关系表,我可以在areaTypes()关系中定义withPivot,然后像这样访问它:

$location->areaType[0]->pivot->areaTests

由于laravel只识别两个表的外键location_idarea_type_id,我必须包含中间表的id来获取AreaTest表的数据

所以在Location模型中,我必须获取该列

public function areaTypes()
{
    // Get the ID of the pivot table to get the poolTests table (connected with ID column)
    return $this->belongsToMany(AreaType::class)
        ->using(AreaTypeLocation::class)
        ->withPivot('id');
}

class LocationAreaType 中,应该将 AreaTests() 改为 public function areaTests() 吗? - bilogic
你会如何为$areaTest->location编写location()关系? - bilogic
@bilogic 抱歉,我意识到我的示例最初并不清楚。我已经相应地编辑了我的答案。 - Robert Tirta

-1

无需为数据透视表创建新模型。 只需在Location模型中声明以下代码:

  /**
   * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function area_types()
  {
        return $this->belongsToMany('App\AreaType', 'area_type_location', 'location_id', 'area_type_id');

  }

并在 AreaType 模型中声明以下代码:

  /**
   * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function locations()
  {
        return $this->belongsToMany('App\Location', 'area_type_location', 'area_type_id', 'location_id');

  }

每次需要在每个控制器中获取区域类型位置时,您可以这样调用函数:$areatype->locations()->get(); 别忘了创建 area_type_location 数据库迁移表。

但我的问题并不是关于area_typeslocations之间的关系,而是关于locations和连接到透视表的area_test的关系。 - Robert Tirta
通过在上述方法中声明关系,我们定义了中间表(area_type_location)的名称和引用ID(area_type_id, location_id),最终,与中间表的连接变得清晰明了 :D - Sara Vaseei
@SaraVaseei,正如原帖中所评论的那样,你的答案没有解释如何查询“area_test”。它甚至没有提到该表。 - Gustavo Straube

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