我正在尝试使用依赖于通知模型的值的关系立即加载用户的通知:
$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
而不是通知模型本身。。。很明显我错过了一些非常琐碎的事情。有什么建议吗?
1条答案
按热度按时间krugob8w1#
你可以利用
Lazy Eager Loading
```$notifications = $user->notifications;
$notifications->each(function ($item) {
if ($item->notifiable_type == 'value-one') {
$item->load('notifiable');
}
});