Skip to content

Instantly share code, notes, and snippets.

@adamwathan
Last active February 22, 2016 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamwathan/c28a1106263ab97ca4e0 to your computer and use it in GitHub Desktop.
Save adamwathan/c28a1106263ab97ca4e0 to your computer and use it in GitHub Desktop.
Dealing with Dependencies in Active Record Models — Option #1: Using a facade/service locator/global access
<?php
class Invitation extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'invited_by');
}
public function send()
{
Mail::send('emails.invitation', ['invited_by' => $this->user], function ($m) {
$m->to($this->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