harunB10
harunB10

Reputation: 5207

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.

Upvotes: 0

Views: 2452

Answers (1)

lewis4u
lewis4u

Reputation: 15047

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

Upvotes: 2

Related Questions