Last active
January 29, 2016 18:43
-
-
Save tonysm/6361b9c7e3e8a0918344 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 | |
class InvitationMailer | |
{ | |
private $mailer; | |
public function __construct(Mailer $mailer) | |
{ | |
$this->mailer = $mailer; | |
} | |
public function sendInvitation(User $inviter, $invitee) | |
{ | |
$this->mailer->send('emails.invitation', ['inviter' => $inviter], function($m) use ($invitee) { | |
$m->to($invitee)->subject('You have been invited to this awesome app'); | |
}); | |
} | |
} |
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 | |
class InvitationsController extends Controller | |
{ | |
public function store(InvitationMailer $mailer) | |
{ | |
$user = auth()->user(); | |
$invitee = request()->get('invitee'); | |
$user->invite($mailer, $invitee); | |
} | |
} |
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 | |
class Invite extends Model | |
{ | |
protected $fillable = ['invitee']; | |
public function inviter() | |
{ | |
return $this->belongsTo(User::class); | |
} | |
} |
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 | |
class User extends Model | |
{ | |
public function invites() | |
{ | |
return $this->hasMany(Invite::class, 'inviter_id'); | |
} | |
public function invite(InvitationMailer $invitation, $invitee) | |
{ | |
$invitation->sendInvitation($this, $invitee); | |
return $this->invites()->create([ | |
'invitee' => $invitee | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment