title: Laravel 5: Call to a member function sum() on null and other persistence issues
body: <p>I have a threaded-comments list each with an upvote/downvote button using <a href="https://github.com/janosgyerik/jquery-upvote" rel="nofollow">jquery.upvote.js</a>.</p>
  
  <p>The voting action is done through ajax and it works, making an upvote or a downvote gets registered in the database with the correct values. Removing the vote will delete the record from the database. So technically it works as intended.</p>
  
  <p>However, I have 2 problems:</p>
  
  <ol>
  <li>The votes that the user has made do not persist after page reload which is important otherwise users won't know what they voted on.</li>
  <li>When I add this chunk of code <code>{{ $each_comment-&gt;commentvotes-&gt;sum('value') }}</code> to the view to grab the sum of the votes on a given comment, I get the following error:</li>
  </ol>
  
  <blockquote>
    <p>Call to a member function sum() on null</p>
  </blockquote>
  
  <p>Routes</p>
  
  <pre><code>Route::resource('votes', 'VotesController');
  Route::resource('commentvotes', 'CommentVotesController');
  </code></pre>
  
  <p>I'd like to point out that I've used the same method successfully on posts' votes with Vote model and VoteController.</p>
  
  <p><code>CommentVote</code> model</p>
  
  <pre><code>class CommentVote extends Model
  {
      protected $table = 'commentvotes';
  
      protected $fillable = [
          'value',
          'comment_id',
          'user_id'
      ];
  
      public function user() {
          return $this-&gt;belongsTo('App\User');
      }
  
      public function posts() {
          return $this-&gt;belongsTo('App\Comment');
      }
  }
  </code></pre>
  
  <p><code>CommentVotesController</code></p>
  
  <pre><code>class CommentVotesController extends Controller
  {
      public function __construct() {
          $this-&gt;middleware('auth', ['only' =&gt; ['create', 'edit'] ]);
      }
  
      public function store(Requests\CommentVoteRequest $request)
      {
          $commentId = $request-&gt;input('commentId');
          $userId = $request-&gt;user()-&gt;id;
          $value = $request-&gt;input('value');
  
          // Check to see if there is an existing vote
          $vote = CommentVote::whereCommentId($commentId)-&gt;whereUserId($userId)-&gt;first();
          if (!$vote)
          {
              // First time the user is voting
              CommentVote::create(['comment_id' =&gt; $commentId, 'user_id' =&gt; $userId, 'value' =&gt; $value]);
          } else {
              $vote-&gt;value == $value ? $vote-&gt;delete() : $vote-&gt;update(['value' =&gt; $value]);
          }
          // AJAX JSON RESPONSE
          return response()-&gt;json(['status' =&gt; 'success',
              'msg' =&gt; 'Vote has been added.']);
      }
  }
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>$(document).ready(function() {
          $('.topic').upvote();
          $('.comment').upvote();
  
          $('.vote').on('click', function (e) {
              e.preventDefault();
              var $button = $(this);
              var postId = $button.data('post-id');
              var value = $button.data('value');
              $.post('http://localhost/r2/public/votes', {postId:postId, value:value}, function(data) {
                  if (data.status == 'success')
                  {
                      // Do something if you want..
                  }
              }, 'json');
          });
  
          $('.commentvote').on('click', function (e) {
              e.preventDefault();
              var $button = $(this);
              var commentId = $button.data('comment-id');
              var value = $button.data('value');
              $.post('http://localhost/r2/public/commentvotes', {commentId:commentId, value:value}, function(data) {
                  if (data.status == 'success')
                  {
                      // Do something if you want..
                  }
              }, 'json');
          });
      });
  </code></pre>
  
  <p>Relevant part of the view <code>comment_list.blade.php</code></p>
  
  <pre><code>@foreach($comments as $each_comment)
  &lt;div class="col-md-1"&gt;
      &lt;div class="upvote comment" data-comment="{{ $each_comment-&gt;id }}"&gt;
          &lt;a class="upvote commentvote {{ $each_comment-&gt;commentvotes &amp;&amp; $each_comment-&gt;commentvotes-&gt;contains('user_id', Auth::id()) ? ($each_comment-&gt;commentvotes-&gt;where('user_id', Auth::id())-&gt;first()-&gt;value &gt; 0 ? 'upvote-on' : null) : null}}" data-value="1" data-comment-id="{{ $each_comment-&gt;id }}"&gt;&lt;/a&gt;
          &lt;!-- Notice how we set the sum of the votes for this post here --&gt;
          &lt;span class="count"&gt;{{ $each_comment-&gt;votes-&gt;sum('value') }}&lt;/span&gt;
          &lt;a class="downvote commentvote {{ $each_comment-&gt;commentvotes &amp;&amp; $each_comment-&gt;commentvotes-&gt;contains('user_id', Auth::id()) ? ($each_comment-&gt;commentvotes-&gt;where('user_id', Auth::id())-&gt;first()-&gt;value &lt; 0 ? 'downvote-on' : null) : null}}" data-value="-1" data-comment-id="{{ $each_comment-&gt;id }}"&gt;&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @endforeach
  </code></pre>