Created
October 27, 2015 19:38
-
-
Save jlem/a32c23e037126ba6e10c to your computer and use it in GitHub Desktop.
Medium Laravel Article - leaky hybrid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment