Skip to content

Instantly share code, notes, and snippets.

@adamwathan adamwathan/Invitation.php Secret
Created Jan 27, 2016

Embed
What would you like to do?
Dealing with Dependencies in Active Record Models — Option #4b: Using optional method injection
<?php
class Invitation extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'invited_by');
}
public function send(Mailer $mailer = null)
{
$mailer = $mailer ?: Mail::getFacadeRoot();
$mailer->send('emails.invitation', ['invited_by' => $this->invitationRecord->user], function ($m) {
$m->to($this->invitationRecord->recipient)->subject("You've been invited to my app!");
});
}
}
<?php
class InvitationsController extends Controller
{
public function store()
{
$invitation = Invitation::create([
'recipient' => request('recipient'),
'invited_by' => auth()->user()->id,
]);
$invitation->send();
return response('', 204);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.