我正试图通过依赖于通知模型值的关系来加载用户的通知:
$notifications = $user->notifications()
->with([
'notifiable' => function ($query) {
// Only load notifiable relation if notification 'notifiable_type' equals...
},
'notifiable.group:id,title' => function ($query) {
// Only load notifiable.group:id,title relation if notification 'notifiable_type' equals...
}
])
->get();
问题是闭包中的$query
正在查询notifiable
关系,而不是通知模型本身……我显然缺少了一些琐碎的东西。有什么建议吗?
答案 0 :(得分:1)
您可以使用Hessian Normal Form
$notifications = $user->notifications;
$notifications->each(function ($item) {
if ($item->notifiable_type == 'value-one') {
$item->load('notifiable');
}
if ($item->notifiable_type == 'value-two') {
$item->load('notifiable.group:id,title');
}
});