Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.3 Send Notification to Users without an account

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:

  1. Am I able to send Notification only to User Model?

  2. Is there a way to send Notifications to new users who doesnt have an account yet?

  3. Is my only choice is to use Laravel's Mailable class instead of Notification in these cases?

like image 314
Neel Avatar asked Nov 24 '16 07:11

Neel


3 Answers

Some alternative solution could be to just new up a user model and set the email attribute without saving it. The instance can now be notified.

I don't see this as the correct approach for this particular case, but for the questions written and since this thread provides some different ways to notify a non-existing user I thought I would write it here.

$invitedUser = new User;
$invitedUser->email = '[email protected]';
$invitedUser->notify(new InvitationNotification());

Beware, that this solution may cause problems when queueing notification (because it doesn't reference a specific user id).

In Laravel >=5.5 it is possible to make an "on-demand notification", which covers this exact case, where you want to notify a non-existing user.

Example from the docs:

Notification::route('mail', '[email protected]')
        ->route('nexmo', '5555555555')
        ->notify(new InvoicePaid($invoice));

This will send the notification to the email address, and that approach can be queued etc.

The docs: https://laravel.com/docs/5.5/notifications#on-demand-notifications

like image 59
thephper Avatar answered Oct 31 '22 15:10

thephper


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.

like image 23
Andreas Elia Avatar answered Oct 31 '22 15:10

Andreas Elia


Notifications are meant to be sent to a notifiable object. You don’t “notify” an invitation.

If all you’re wanting to do is email someone that they’ve been invited to use an application, then just send an email to that email address. Notifications aren’t appropriate in this instance.

like image 3
Martin Bean Avatar answered Oct 31 '22 15:10

Martin Bean