app/Models/Traits/Viewable.php
/**
     * Add a view for this record by the given user.
     *
     * @param $userId mixed - If null will use currently logged in user.
     */
Add a view for this record by the given user.
@param $userId mixed - If null will use currently logged in user.
Add a view for this record by the given user . @param $userId mixed - If null will use currently logged in user .
public function view($userId = null)
    {
        if (is_null($userId)) {
            $userId = auth()->id();
        }
        if ($userId) {
            $view = $this->views()
                         ->where('user_id', '=', $userId)
                         ->first();
            if ($view) {
                $view->touch();
                return;
            }
            $view          = new View();
            $view->user_id = $userId;
            $this->views()->save($view);
        }
        $this->increment('views_count');
    }
public function view ( $ userId = null ) { if ( is_null ( $ userId ) ) { $ userId = auth ( ) -> id ( ) ; } if ( $ userId ) { $ view = $ this -> views ( ) -> where ( 'user_id' , '=' , $ userId ) -> first ( ) ; if ( $ view ) { $ view -> touch ( ) ; return ; } $ view = new View ( ) ; $ view -> user_id = $ userId ; $ this -> views ( ) -> save ( $ view ) ; } $ this -> increment ( 'views_count' ) ; }
Add a view for this record by the given user.
Add a view for this record by the given user .