简体   繁体   中英

Laravel - Send email when new entry is inserted in db?

I have a case where users can save their searches (it is saved in a table with user id and id of the searched item). What I really want is to send an email to that user every time when new item is inserted in the database. For example, if user saved search for PlayStation 4, I want to alert the user (via email) that new article about PS4 is released. Articles are stored in another table.

At the moment I'm not sure how to start. I've been reading official docs about Events and Handlers, but it's not still clear to me... Btw. I'm using Laravel 5.3 and Mailgun for emails.

This is a perfect use case for laravel notifications

// create new notification class
php artisan make:notification ArticlePublished

// to edit the view run this
php artisan vendor:publish --tag=laravel-notifications

and then when a new article is stored just add this line of code in your controller store method:

$user->notify(new ArticlePublished);

Here is a great video about that: https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM