Kryptonit3-zz/Counter

Same result

Opened this issue · 5 comments

Hi, your counter show the same result in foreach. I use Counter::show('user-profile', $user->id), it gets first result in foreach

I have the same problem. I count when users see throw route "post/post_id"

when i list in another page to show all post and its counter i have the same result for all.

@Kryptonit3 I have the same problem.

So for posts you are counting with Counter::count('post', $post->id); when viewing the actual post. (remember, for dynamic pages the counter identifier needs to be unique from other pages. so you use your identifier and pass in the unique value for this page).

Then when you show the lists of posts with their views I assume you are doing this:

$posts = App\Post::all();

return view('all-posts', compact('posts'));

then in blade

@foreach($posts as $post)
    <li> this is a post - View Count: {{ Counter::show('post', $post->id) }}</li>
@endforeach

Hi, same problem here (Laravel v5.1.29), i did try as suggested @Kryptonit3, show the same result in the foreach.

I think it might work :D
I try to edit \vendor\kryptonit3\counter\src\Counter.php
by changing function private static function createPageIfNotPresent($page)
from

private static function createPageIfNotPresent($page)
{
    if (!isset(self::$current_page)) {
        self::$current_page = Page::firstOrCreate([
            'page' => $page
        ]);
    }

    return self::$current_page;
}

to

private static function createPageIfNotPresent($page)
{
    return self::$current_page = Page::firstOrCreate(['page' => $page]);
}