【发布时间】:2019-02-07 01:47:40
【问题描述】:
我有以下表格:
-
contacts(id, name, email, phone_no), -
events(id, title, start_date, end_date), -
addresses(id, city, state, country, zip_code), addressables(id, address_id, addressable_id, addressable_type)
这里addressables 与contacts events 有多对多多态关系。 addressables 还可以容纳其他多态模型类。
地址模型
public function addressable()
{
return $this->morphTo();
}
事件/联系模型
public function addresses()
{
return $this->morphToMany(Address::class, 'addressable');
}
我想获取地址及其相关模型。如果有人可以帮助我,我将不胜感激
- 列出具有关联模型的地址
- 列出特定型号类型的地址
- 按型号类型列出地址分组
更新
预期结果:
地址
id | address
1 | New York, USA
2 | California, USA
3 | Nevada, USA
联系人
id | name
1 | Albert King
2 | Michelle Johnson
3 | Sujit Baniya
活动
id | title
1 | Event 1
2 | Event 2
3 | Event 3
可寻址对象
id | address_id | addressable_id | addressable_type
1 | 1 | 1 | Contact
2 | 2 | 1 | Event
3 | 3 | 2 | Contact
Address::with('addressables') 的异常结果
Address - ID 1
----Details (New York, USA)
----Contact (Albert King)
Address - ID 2
----Details (California, USA)
----Event (Event 1)
Address - ID 3
----Details (Nevada, USA)
----Contact (Michelle Johnson)
提前谢谢你!
【问题讨论】: