php - How to count number of views for a Post (Laravel Event Listener)? -


i trying count number of views products. there column view_countin products table. trying implement event listener.

this event code :

productwasviewed.php

<?php namespace app\events; use app\events\event; use app\modules\product\models\product; use illuminate\queue\serializesmodels; use illuminate\contracts\broadcasting\shouldbroadcast;  class productwasviewed   {     use serializesmodels;     public $product;      public function __construct(product $product)     {         $this->product = $product;     }      public function broadcaston()     {         return [];     } } 

this listener code

incrementproductviewcount.php

<?php namespace app\listeners; use app\events\productwasviewed; use illuminate\queue\interactswithqueue; use illuminate\contracts\queue\shouldqueue; use app\modules\product\models\product;  class incrementproductviewcount {     public function __construct()  { }      public function handle(productwasviewed $event)     {         $event->product->increment('view_count');     } } 

eventserviceprovider.php array:

'app\events\productwasviewed' => [             'app\listeners\incrementproductviewcount',         ], 

controller used event listener:

public function singleproduct(request $request) {     $name = $request->name;      $product = db::table('products')                                 ->where('name' , '=', $name)                 ->where('status','=',1)             ->first();       event::fire(new productwasviewed($product));  } 

the error getting:

type error: argument 1 passed app\events\productwasviewed::__construct() must instance of app\modules\product\models\product, instance of stdclass given

how resolve problem? please help.

query builder request returns stdclass object. need use eloquent here product object:

$product = product::where('name', $name)->where('status', 1)->first();  

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -