Skip to content

Instantly share code, notes, and snippets.

@ShawnMcCool
Last active September 15, 2015 18:34
Show Gist options
  • Save ShawnMcCool/85efa9a6552d7f867bcf to your computer and use it in GitHub Desktop.
Save ShawnMcCool/85efa9a6552d7f867bcf to your computer and use it in GitHub Desktop.
<?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([
// ...
]);
@adamwathan
Copy link

@zackkitzmiller I usually keep the happy path outdented and guard the less common cases up front, but not religious about it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment