我对laravel 5.5中的多对多关系有疑问
我将这些表格命名为ads
,attributes
,categories
,ads_attributes
categories
与ads
有一对多,attributes
和ads
和attributes
在ads_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')
有什么想法吗?
对不起我的英语不好,这不是我的母语