I am converting a piece of database SQL query to Laravel Eloquent query but it doesn’t work.
My code snippet looks like this:
SELECT user_id FROM posts WHERE user_id NOT IN (SELECT id FROM users)
Can someone explain why?
Advertisement
Answer
So assuming you have a Post
model, you can try something like this:
Post::select('user_id')->whereNotIn('user_id', User::pluck('id'))->get();