Skip to content
Advertisement

How can this be witten in Laravel Eloquent term?

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();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement