Last active
September 15, 2015 18:34
-
-
Save ShawnMcCool/85efa9a6552d7f867bcf to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Discussion about idiomatic ActiveRecord usage | |
## Please help me to determine the idiomatic approach for creating a member and attaching an invoice ONLY IF the member doesn't already have 5 invoices. | |
class Member extends Eloquent { | |
} | |
$member = Member::create([ | |
'email' => $email, | |
'password' => $password, | |
]); | |
// Then later, when they are adding invoices | |
// my gut says that this isn't idiomatic.. suggestions? | |
if ($member->invoices()->count() >= 5) { | |
throw new MemberCannotHaveMoreThanFiveInvoices($member->id); | |
} | |
$member->invoices()->create([ | |
// ... | |
]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@zackkitzmiller I usually keep the happy path outdented and guard the less common cases up front, but not religious about it 👍