0

I have 2 tables Articles and Revisions
There is a relationship between them one2many

The Revisions table has 2 columns isApproved and isPublished

Now, I want to select all articles, when its first Approved revision is Published

Article::whereHas('revisions', function ($query) {
    $query->where('isApproved', 1)->where('isPublished', 1);
});

But this code selected all articles that have approved and published revisions

user9500574
  • 142
  • 1
  • 11

1 Answers1

1

Try filter the results:

$articles = Article::
               whereHas('revisions', function ($query) {
                   $query->where('isApproved', 1)->where('isPublished', 1);
               })
               ->get()
               ->filter(function ($article) {
                    return $article->revisions->first(function ($revision) {
                               return $revision->isApproved == true;
                           })
                           ->isPublished == true;

               });
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71