An example of model observers
First of all, here’s how you can do the same thing you did in the first model events example, using observers.
Create a new file named WelcomeUserObserver.php
under app/Observers
. Now, type in the following:
<?php namespace AppObservers; class WelcomeUserObserver { public function created($user){ Mail::send('emails.welcome', ['user' => $user], function($message) use ($user) { $message->to($user->email, $user->first_name . ' ' . $user->last_name)->subject('Welcome to My Awesome App, '.$user->first_name.'!'); }); } }
Then, you can register the observer in the boot()
method of EventServiceProvider
:
/** * Register any other events for your application. * * @param IlluminateContractsEventsDispatch
Comments