Laravel's Events and Observers (5) first, we will show you how to implement the first example of a previous pattern event through model observer.
Create the WelcomeUserObserver. php file in the app/Observers folder and add the following code:
$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 register the observer in the boot () method of EventServiceProvider:
/** * Register any other events for your application. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function boot(DispatcherContract $events) { parent::boot($events); User::observe(new WelcomeUserObserver); }
This is OK! Now your observer has been associated with the model.
Let's assume another situation. The librarians put forward some new requirements for the code:
- When a new author is added, each user receives a notification.
- Each time you add or delete an author, an email is sent.
- Finally, every time a book is deleted, the librarians need to know how many authors in the database do not have relevant books.
Now let's get started. We need three separate classes (remember ourSingle responsibility principle): CustomerNewAuthorObserver, LibrarianAuthorObserver, and AuthorsWithoutBooksObservers.
Note: you can name these classes as you like. here we just select a name that is easier to associate with the selected behavior.
Here we create three classes respectively:
Now we should add some logic. First, add the following for CustomerNewAuthorObserver:
$author], function($message) use ($user) { $message->to($user->email, $user->first_name . ' ' . $user->last_name)->subject('New Author Added!'); }); } } }
Note: I know this is a very simple and crude method. here we only want to achieve the above purpose. In practice, you can use the mail queue.
$author], function($message) use ($author) { $message->to('librarian@awesomelibrary.com', 'The Librarian')->subject('New Author: ' . $author->first_name . ' ' . $author->last_name); }); } public function deleted($author) { Mail::send('emails.deleted_author_librarian', ['author' => $author], function($message) use ($author) { $message->to('librarian@awesomelibrary.com', 'The Librarian')->subject('New Author: ' . $author->first_name . ' ' . $author->last_name); }); } }
Finally:
get(); if(count($authorsWithoutBooks) > 0){ Mail::send('emails.author_without_books_librarian', ['authorsWithoutBooks' => $authorsWithoutBooks], function($message) { $message->to('librarian@awesomelibrary.com', 'The Librarian')->subject('Authors without Books! A check is required!'); }); } } }
Note: As mentioned above, we assume that you have learned the basic knowledge of sending emails from Laravel. if not, you can go to the official website to learn more.
It does not end here. You can use Laravel's model events and model observer in a large number of cases and scenarios. For example, if you write a blog and want to update some site maps every time you publish a new article or update an original article, you can use the observer. For example, when you add a new book, you can also use the observer to record something.
This article is a special topic: Laravel's Events (Events) and Observers (observer)
- Next Article: Laravel's Events and Observers (6)-Summary
- Previous article: Laravel's Events and Observers (4)-model Observer