I’m trying to retrieve all records of all posts that haven’t been seen yet for a particular user, but I can’t seem to get it right. When I run the raw query I always get an return and if I change the post.ownerId => 9
and the posts
.is_public => 30
it still returns records but I don’t have posts in the post table
I’ve made the next sql script:
select `posts`.`id` from `posts` left join `post_seens` on `posts`.`id` = `post_seens`.`postId` and `post_seens`.userId = 2 and `posts`.`ownerId` = 2 and `posts`.`is_public` = ? where `post_seens`.`postId` is null and `post_seens`.`userId` is
null limit 500;
And with this PHP / Laravel code:
$items = DB::table('posts')->leftJoin('post_seens', function ($join) use ($user, $postOwner) {
$join->on('posts.id', '=', 'post_seens.postId')
->where('post_seens.userId', '=', $user->id)
->where('posts.ownerId', '=', $postOwner)
->where('posts.is_public', '=', 1);
})
->whereNull('post_seens.postId')
->whereNull('post_seens.userId')
->select(['posts.id'])
->limit(500)->get();
I wanted to write a matches, so if the records exists in the post_seen table and some visit some xprofile
again, only the new posts are added to the post_seen table.
(This is it without escaping)
select `posts`.`id` from `posts` left join `post_seens` on `posts`.`id` = `post_seens`.`postId` and `post_seens`.userId = 2 and `posts`.`ownerId` = 15 and `posts`.`is_public` = 30 where `post_seens`.`postId` is null and `post_seens`.`userId` is
null limit 500;
What did I do wrong