Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

onyx26's avatar

Eloquent whereNotIn

Could be my misunderstanding, but I thought you could use all Query Builder constraints with Eloquent. Unfortunately whereNotIn doesn't seem to be working for me and gives the error: "Method whereNotIn does not exist"

I have two Models: Users and Logs. Logs links to an application log table, and a one-to-many relationship links the Users to the Logs. The following SQL query works fine in phpMyAdmin:

SELECT COUNT(*)
FROM `logs`
WHERE sub_class = 'K'
AND year = '2016'
AND week = '3'
AND table_name = 'PCPLEVNT'
AND field_name = 'SUBSTATUS'
AND old_value IS NULL
AND new_value NOT IN ('RESOLVED', 'COMPLETED')

Sadly I can't seem to get this to work as an Eloquent query:

User::find($id)->logs
    ->where('year', $year)
    ->where('week', $week)
    ->where('sub_class', 'K')
    ->where('field_name', 'SUBSTATUS')
    ->where('old_value', NULL)
    ->whereNotIn('new_value', ['RESOLVED', 'COMPLETED'])
    ->count()

Have I done something wrong? Is there a better way to do this? I know you can use DB::, but since I already have a model relationship set up I thought it best to use it.

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You're using a Collection after this point User::find($id)->logs change it to a Builder instance by using User::find($id)->logs() and continue chaining

onyx26's avatar

Thank you so much - I had no idea about that. Still learning!

Please or to participate in this conversation.