1
I’m using the event Registered of Laravel himself (Illuminate Auth Events Registered) to fire a Listener I created to fire a welcome email.
See how the Listener turned out:
<?php
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
// instância do evento registered do próprio Laravel
use Illuminate\Auth\Events\Registered;
use Illuminate\Notifications\Notifiable;
use App\Notifications\EnviarEmailBoasVindasNotification;
class EnviarEmailBoasVindas
{
use Notifiable;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NovoUsuario $event
* @return void
*/
public function handle(Registered $event)
{
// No "envelope" $event podemos acessar a instância User assim $event->user
$event->user->notify(new EnviarEmailBoasVindasNotification($event));
}
}
My doubt is on account of "use Illuminate Notifications Notifiable" and by the occurrence "use Notifiable" already within the class. What is the use of these? From what I have seen to remove both will continue to function normally.
There’s something that doesn’t match your code, because, these
namespace
andtraits
may not be required there, but may be required in the class within the methodnotify
. You followed the documentation I did something else?– novic
Everything correct, at first I followed the documentation as it should be. See if I’m correct, $Event has an instance of the User model, within this model I have the occurrence use Illuminate Notifications Notifiable;, this would be the reason why I don’t need to have a new occurrence in the above file?
– Fábio Jânio
Where do you call
EnviarEmailBoasVindas
?– novic
I have an Event that calls this Liener (Send Emailboasvindas) there.
– Fábio Jânio
I would make a
notification
and aobservers
? I think it becomes more logical.– novic
In fact Virgilio, in this case it is better to use Observers. I did not know this option. However, I believe that in the above case, my understanding is correct in imagining that I do not need to instantiate the notification class again since it is instantiated in the User model
– Fábio Jânio
I could not understand what you did because I have done it and I have examples and need yes, from what I could perceive or imagine there are two classes calling each other! good is assumption. but, in your case it is better an Observer with method created with notification!
– novic