我的数据库中有 2 个表,一个是 notifications
表,一个是 notifications_user
表。
一个通知可以有多个通知用户。在我的通知模型中,我有以下关系的结果,
public function user()
{
return $this->belongsToMany('User')->withPivot('is_read');
}
我想要实现的是获取所有未读通知(或 is_read = 0),但 is_read 列在通知用户表中,因此我不知道如何对其运行查询。
目前我有这个,
$unread = Notification::has('user')->with('user')->get();
现在这将关系拉入结果,但不考虑 is_read 值。
有没有一种方法可以根据 1:n 关系中的值从表中选择所有数据。
最佳答案
您可以通过执行以下操作来查询关系:
$unread = Notification::has('user')->with('user')->where('is_read','!=', 0)->get();
查看文档:http://laravel.com/docs/5.1/eloquent-relationships#querying-relations
关于php - Eloquent 查询 1 :n relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31612083/