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();
1
  • 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" Commented Sep 26, 2014 at 9:42

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.