The way I did this is querying a model Post
with a relation whereDoesnthave
. In the controller:
public function getPostsWithoutCommenter(){
$userId = 1;
$posts = \App\Post::whereDoesntHave("comments", function($subQuery) use($userId){
$subQuery->where("user_id", "=", $userId);
})->get();
}
This suggests that it comments
is defined in the model Post
as follows:
public function comments(){
return $this->hasMany(Comment::class);
}
, comments
$userId
a Collection
, .