dongtiao0657 2019-07-04 08:41
浏览 87

Laravel模型关系 - 我想创建什么样的关系?

I have got 3 models associated with this question; Country, Manufacturer and Region.

I've simplified the tables for the sake of this question. I don't think anything else in the tables or any of the other models are anything to do with the problem.

My tables are set up like so;

manufacturers
 - id
 - name

countries
 - id
 - name

regions
 - id
 - name
 - manufacturer_id
 - country_id

What I want to be able to do is to write $manufacturer->countries in my blade and have it spit out the countries that are associated with a given manufacturer.

The models are currently related to each other like this;

Country.php

public function manufacturers()
{
    return $this->hasMany(Manufacturer::class);
}

public function regions()
{
    return $this->hasMany(Region::class);
}

Region.php

public function manufacturer()
{
    return $this->belongsTo(Manufacturer::class);
}

public function country()
{
    return $this->belongsTo(Country::class);
}

and where I'm having the problem, Manufacturer.php

I think I need a hasMany relationship. I've already got;

public function regions()
{
    return $this->hasMany(Region::class);
}

and I would have thought I would have needed;

public function countries()
{
    return $this->hasManyThrough(Country::class,Region::class);
}

but that leads to this error;

Column not found: 1054 Unknown column 'countries.region_id' in 'on clause' (SQL: select `countries`.*, `regions`.`manufacturer_id` as `laravel_through_key` from `countries` inner join `regions` on `regions`.`id` = `countries`.`region_id` where `regions`.`manufacturer_id` = 4)

so I tried swapping the classes round to give;

public function countries()
{
    return $this->hasManyThrough(Region::class,Country::class);
}

but that leads to;

Column not found: 1054 Unknown column 'countries.manufacturer_id' in 'field list' (SQL: select `regions`.*, `countries`.`manufacturer_id` as `laravel_through_key` from `regions` inner join `countries` on `countries`.`id` = `regions`.`country_id` where `countries`.`manufacturer_id` = 4)

Does anyone know how I should be setting my relationships up in order to achieve what I want?

I also tried a belongsToMany relationship, which did bring back the countries, but multiple instances of the same country. I just want one instance of each country that appears in the regions table for any given manufacturer.

  • 写回答

2条回答 默认 最新

  • dongluojiao6322 2019-07-04 08:49
    关注

    You are actually dealing with the many-to-many relationship.

    In your case regions is a pivot table.

    Kindly check belongsToMany.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c