0

I want to save comments, posts and profiles that a user views in a viewable table. Posts and comments are displayed in a stream similar to social networks. How can I store the data in a viewable table in the background?

Table:

viewables
    user_id
    viewable_id
    viewable_type

For example, if a user views or sees a post in the stream, the database should store the user_id and post_id. The same should be possible with comments and profiles.

EDIT:

The posts are issued in a forelse loop and the comments in a foreach.

 @forelse ($posts as $post)
            @if ($post->type === 1)
{{$post->body}}
                @foreach ($post->comments as $comment)          
                   {!!$comment->body!!}
                @endforeach
            @elseif ($post->type === 2)
{{$post->image}}
                @foreach ($post->comments as $comment)          
                   {!!$comment->body!!}
                @endforeach
            @elseif ($post->type === 3)
show post with type 3
            @elseif ($post->type === 4)
show post with type 4
            @else
show remaining
    @endforelse
slickness
  • 246
  • 7
  • 21
  • What you mean view? Do you have links for each comment? Maybe trigger a ajax event when a user click to see a view/comment – Amitoz Deol Sep 27 '18 at 20:04
  • No, I have no extra views. For example, if a user views or sees a post in the stream, the database should store the user_id and post_id. The same should be possible with comments and profiles. – slickness Sep 27 '18 at 20:14
  • So you are looking to save the user id of the person reading the post(s)? Will need more info on how your view is set up with the posts. – JPark Sep 27 '18 at 21:25
  • @JPark I added an exampleview to my post – slickness Sep 28 '18 at 19:24

1 Answers1

1

Based on your edit, there are a few different ways to handle this. Technically, since all posts are included on the page, it could be assumed that the user has read them all and simply save the user id for all of the posts loaded before rending the view.

If you wanted something more refined, however, you could use an ajax method to post the user id, post id, etc. when scrolling over it, though I don't find that very practical, as that could cause problems if someone scrolls through too fast.

The best option (without requiring user interaction) would be to load a couple posts at a time. Either automatically when scrolling (see this answer), or with the help of Laravel's pagination (https://laravel.com/docs/5.7/pagination) and set the user id when loading the data.

Example:

$posts = Posts::paginate(5);

foreach($posts as $p) {
    DB::table('viewables')->insert([
        'user_id' => $user->id,
        'viewble_id' => $p->id,
        `viewable_type` => $p->type,
    ]);
}
JPark
  • 779
  • 4
  • 6
  • thanks for your answer. For exampel, if 10 posts are loaded and the User read only 2 posts that are saved as read every 10 posts. I think thats not a good solution. – slickness Sep 29 '18 at 07:07
  • Well, it isn't without it's drawbacks. Ideally, you should have a way for the user to click to read the post in order to store an accurate count. – JPark Oct 01 '18 at 14:47