laravel上的多对多关系

时间:2018-02-08 07:56:02

标签: mysql laravel many-to-many

我对laravel 5.5中的多对多关系有疑问

我将这些表格命名为adsattributescategoriesads_attributes categoriesads有一对多,attributes

也是如此

adsattributesads_attributes

中是多对多相关的

问题是:attributes中的主键是['name', 'category_id'](我知道它很愚蠢,但这是实现atm并且我不会重组我的表格)

所以在ads_attributes我有以下列:['attribute_name', 'category_id', 'ad_id']

Laravel belongsToMany()方法给了我这个:

select `attributes`.*, `ads_attributes`.`ad_id` as `pivot_ad_id`, `ads_attributes`.`attribute_name` as `pivot_attribute_name`, `ads_attributes`.`value` as `pivot_value` from `attributes` inner join `ads_attributes` on `attributes`.`name` = `ads_attributes`.`attribute_name` where `ads_attributes`.`ad_id` in ('11')

虽然我需要类似的东西:

select `attributes`.*, `ads_attributes`.`ad_id` as `pivot_ad_id`, `ads_attributes`.`attribute_name` as `pivot_attribute_name`, `ads_attributes`.`value` as `pivot_value` from `attributes` inner join `ads_attributes` on `attributes`.`name` = `ads_attributes`.`attribute_name` and `attributes`.`category_id` = `ads_attributes`.`category_id` where `ads_attributes`.`ad_id` in ('11')

有什么想法吗?

对不起我的英语不好,这不是我的母语

0 个答案:

没有答案