I know that spark has events that can be listened to when user has registered but I'm totally new to laravel and Events, are there examples that I can make use of to access the events? My goal is to listen to the user created event and send a welcome email to the user.
-
Hi if you don't want to listen the event, you may try `$user->save()` and then **mail function** to send the mail where you can use information of the user who from the variable `$user` only. And I think there is no need to use event for this. – Siddharth May 02 '16 at 07:53
-
I want to send welcome email to the registered user,once he/she is registered. what happen is that upon registration its going to register() method in auth controller of laravel code , i want to overwrite this method send mail from the overwritten method or either if i can add some hook into spark events on register and i hope i am making sense if any body has simple solution please share. – Mohammed Abrar Ahmed May 03 '16 at 10:09
1 Answers
Finally, here i came up with solution.
Basically, Events call listeners that is defined in the EventServiceProvider class that is store in the providers inside the app folder of the application.
In the EventServiceProvider.php find
'Laravel\Spark\Events\Auth\UserRegistered' => [
'Laravel\Spark\Listeners\Subscription\CreateTrialEndingNotification',
],
it will be store in the $listen of the EventServiceProvider class, this means that the UserRegistered event will call the CreateTrialEndingNotification listener, so we need to create a listerner and attach here , creating listener is easy just create a new file with name HookRegisteredUser(or your choice) some thing like below in app/Listeners sand add its path into the $listen of the "Laravel\Spark\Events\Auth\UserRegistered"
namespace App\Listeners;
use Laravel\Spark\Events\Auth\UserRegistered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class HookRegisteredUser
{
/**
* Handle the event.
*
* @param UserRegistered $event
* @return void
*/
public function handle(UserRegistered $event)
{
//your code goes here
}
}
After this add HookRegisteredUser listener in EventServiceProvider.php as follows,
'Laravel\Spark\Events\Auth\UserRegistered' => [
'Laravel\Spark\Listeners\Subscription\CreateTrialEndingNotification',
'App\Listeners\HookRegisteredUser',
],
Now the UserRegistered event will call two listeners i.e CreateTrialEndingNotification , HookRegisteredUser and the method handle will get executed on call to listeners and thats it!
- 2,290
- 3
- 15
- 33
-
I'm using this too but my User object only contains the tax_rate value and nothing else. Have you managed to pass the full user to the event listener? – Ben Aug 24 '18 at 09:31