Friday, March 16, 2018

Counting Views of Articles - development

Counting Views of Articles

I created a simple application that counts the views of an article. In the database articles table has a column view_count. Also I have a defined event & listener that increments view_count every time when some article is viewed:

The question, how to make update view_count , and store in database..?!

AppEvents ArticleViews.php:

class ArticleViewsextends Event
{
    use SerializesModels;

    public $article;

    public function __construct(Article $article)
    {
        $this->article = $article;
    }

    public function broadcastOn()
    {
        return [];
    }
}

AppListeners IncrementArticleViewsCounts.php:

class IncrementArticleViewsCounts
{
    public function __construct()  { }

    public function handle(ArticleViewsextends $event)
    {
        $event->article->increment('view_count');
    }
}

And in ArticleController.php@show:

$articles= new Scholarship();

Event::fire(new ArticleViewsextends ($scholarships));

parent::$data['articles'] = $articles->getAllActiveArticlesForFrontEnd(parent::$data['language']);

In views don’t count.



from Laravel Questions and Answers https://laravelquestions.com/php/counting-views-of-articles/
via Lzo Media

No comments:

Post a Comment