0

I have a table named answers and its columns are id, answers, batch, candidate_id. I want to get all candidates that has no record of answers under batch number of 1.

Is there a way to add condition to this statement (this is in my candidate model) ?

return $this->has('answers', '=', 0)->get();

I tried this way but it didn't work:

return $this->has('answers', '=', 0)->whereBatch(1)->get();
jpcanamaque
  • 1,049
  • 1
  • 8
  • 14
jedcgc
  • 11
  • 1
  • 3
  • found on documentation "If you need even more power, you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries" – Nabin Kunwar Sep 26 '14 at 09:42

1 Answers1

3

You need whereHas:

$this->whereHas('answers', function ($q) {
   $q->whereBatch(1);
}, '=', 0)->get();

Btw this is exactly the same as calling has with closure as 5th param:

$this->has('answers', '=', 0, 'and', function ($q) {
   $q->whereBatch(1);
})->get();
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157