在我的Laravel项目中,我遇到以下问题。 (为了便于阅读,我减少了输出量)
Collection {#216 ▼
#items: array:7 [▼
0 => NewsPost {#227 ▶}
1 => NewsPost {#228 ▶}
2 => NewsPost {#229 ▼
#attributes: array:6 [▼
"id" => 6
"title" => "Sample title"
"body" => "<p>Some html</p>"
"user_id" => 15
]
#original: array:6 [▶]
#casts: []
.
.
#guarded: array:1 [▶]
}
3 => NewsPost {#230 ▶}
4 => NewsPost {#231 ▶}
]
}
Collection {#219 ▼
#items: array:2 [▼
0 => UserRead {#210 ▼
#attributes: array:4 [▼
"user_id" => 15
"post_id" => 6
]
#original: array:4 [▶]
#casts: []
.
.
#guarded: array:1 [▶]
}
1 => UserRead {#217 ▶}
]
}
答案 0 :(得分:1)
首先获取所有readed post id,然后使用过滤功能过滤未读帖子
$readedPosts = $user_read->pluck('post_id');
$unread_posts = $all_posts->filter(function($value) use ($readedPosts) {
return !in_array($value->id, $readedPosts);
})