lundi 31 juillet 2017

Counting page views with Laravel

I want to implement page view counter in my app. What I've done so far is using this method :

public function showpost($titleslug) {
        $post = Post::where('titleslug','=',$titleslug)->firstOrFail();
        $viewed = Session::get('viewed_post', []);
        if (!in_array($post->id, $viewed)) {
            $post->increment('views');
            Session::push('viewed_post', $post->id);
        }
        return view('posts/show', compact('post', $post));
    }

I retrieve the popular posts list like this :

$popular_posts = Post::orderBy('views', 'desc')->take(10)->get();

However, I'd like to know if there are any better ways to do this ? And with my current method, can I get a list of most viewed posts in the past 24 hours ? That's all and thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire