Skip to content

Instantly share code, notes, and snippets.

@tonysm
Last active January 29, 2016 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonysm/6361b9c7e3e8a0918344 to your computer and use it in GitHub Desktop.
Save tonysm/6361b9c7e3e8a0918344 to your computer and use it in GitHub Desktop.
<?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');
});
}
}
<?php
class InvitationsController extends Controller
{
public function store(InvitationMailer $mailer)
{
$user = auth()->user();
$invitee = request()->get('invitee');
$user->invite($mailer, $invitee);
}
}
<?php
class Invite extends Model
{
protected $fillable = ['invitee'];
public function inviter()
{
return $this->belongsTo(User::class);
}
}
<?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