Skip to content

Instantly share code, notes, and snippets.

@jlem
Created October 27, 2015 19:38
Show Gist options
  • Select an option

  • Save jlem/a32c23e037126ba6e10c to your computer and use it in GitHub Desktop.

Select an option

Save jlem/a32c23e037126ba6e10c to your computer and use it in GitHub Desktop.

Revisions

  1. Jon LeMaitre created this gist Oct 27, 2015.
    20 changes: 20 additions & 0 deletions ControllerExample.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?php

    DemonstrationController
    {
    public function createPost()
    {
    // validate request, create the post, and...

    $member = Auth::user()->member();
    $member->incrementPostCount();
    }

    public function deletePost()
    {
    // validate request, delete the post, and...

    $member = Auth::user()->member();
    $member->decrementPostCount();
    }
    }
    23 changes: 23 additions & 0 deletions Model.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?php

    class Member extends Model implements MemberInterface
    {
    //...


    public function incrementPostCount()
    {
    $this->{self::ATTR_POST_COUNT}++;
    $this->save();
    }

    public function decrementPostCount()
    {
    if ($this->getPostCount() > 0) {
    $this->{self::ATTR_POST_COUNT}--;
    $this->save();
    }
    }

    // ...
    }