lundi 2 août 2021

Laravel eloquant 'whereDoesntHave' get posts not only commented by one user

I need to get the posts, without the posts which only commented by specific user.

Relation in Post model,

public function comments(){
  return $this->hasMany(Comment::class);
}

This is my code,

public function getPostsWithoutCommenter(){
  $userId = 1;
  $posts = \App\Post::whereDoesntHave("comments", function($subQuery) use($userId){
    $subQuery->where("user_id", "=", $userId);
  })->get();
}

This code omits 'Posts which a specific User has commented'. But I want to "omit posts Only that user commented". Which means if some other user also commented I want that in resulset. Is there an eloquant way to do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire