With the Laravel 5.3 Notification feature, I see that the notifications are sent to users like this:
$user->notify(new InvoicePaid($invoice));
Where I believe $user
is the notifiable entity. What if I want to send notifications to users who doesn't have an account yet? In my app, users send invitation to their friends to join. I am trying to use Laravel's Notification to send the invite code to users who don't have an account yet.
When users invite someone, their data is stored in Invite Model like this:
class Invite extends Model
{
protected $table = 'invites';
protected $fillable = array('name', 'email', 'invite_code', 'expires_at', 'invited_by');
protected $dates = ['expires_at'];
}
I thought I can use notify to send notifications to the Invite model like this:
$invite = Invite::find($inviteId);
$invite->notify(new InvitationNotification(ucfirst($invite->name), $invite->invite_code, $invite->expires_at));
But the above doesnt work. I get the error:
Call to undefined method Illuminate\Database\Query\Builder::notify()
So my question is:
Am I able to send Notification only to User Model?
Is there a way to send Notifications to new users who doesnt have an account yet?
Is my only choice is to use Laravel's Mailable class instead of Notification in these cases?
I found your post looking for the same thing but answered it myself.
You need to use Notifiable;
on the Invite
model.
You then import use Illuminate\Notifications\Notifiable;
and should be able to use ->notify
on the Invite
model.
$invite = Invite::create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'token' => str_random(60),
]);
$invite->notify(new UserInvite());
That is how I handle sending the notification :)
You can then pass through $invite
and use that within the notification template.