Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I check if an auth user or a visitor has already viewed a post? #279

Open
Fajendagba opened this issue Nov 15, 2022 · 4 comments
Open

Comments

@Fajendagba
Copy link

I need to be able to know if the current visitor has already viewed a post or not. I want this to be able to sort post orderBy post not yet viewed.

I also need to know if the current Auth user has already viewed a post, so that post can be sorted by post not yet viewed.

I've read many blogs and articles, but found none useful.

@iamB0rgy
Copy link

iamB0rgy commented Jan 5, 2023

I need to be able to know if the current visitor has already viewed a post or not. I want this to be able to sort post orderBy post not yet viewed.

I also need to know if the current Auth user has already viewed a post, so that post can be sorted by post not yet viewed.

I've read many blogs and articles, but found none useful.

Hi @Fajendagba, are you able to get it? I need the same requirement.

@Fajendagba
Copy link
Author

No @iamB0rgy I haven't seen the solution. But in case you get it first, please drop it here. Thank you.

@Rubdui
Copy link

Rubdui commented Mar 23, 2023

I am also searching for the same , any solutions?

@Fajendagba
Copy link
Author

@iamB0rgy @iamB0rgy the solution is actually simpler than I thought. Here it is

public function home(Request $request, Visitor $visitor)
{
    $visitorId = $visitor->id();
    
    // Get all post that hasn't been viewed by the visitor yet
    // Make sure your Post model implements Viewable
    $postsNotYetViewed = Post::whereIn('id', $posts->pluck('id'))
        ->whereDoesntHave('views', function ($query) use ($visitorId) {
            $query->where('visitor', $visitorId);
        })
        ->orderBy('created_at', 'desc')
        ->paginate($limit);

    return view('post', [
        'posts' => $posts,
    ]);
}

MirrorVlogger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants