samedi 25 mars 2017

Laravel 5.3 - Laravel DB transaction and notification sends email on failed transaction?

Using the below transaction, I fire an event, which runs the two listeners listed below the event. The issue is when the first event AddQuestionToQuestionsTable fails for any reason, the DB data gets rolled back correctly because of DB::rollback, but the laravel notification email I have setup and fired in the second listener QuestionAddedNotificationSend gets sent out regardless of the error, so whether there was an error or not.

If there was an error in the transaction, we would not want to send the email. Note: I may add additional listeners that also insert into the DB, so I would need to know how to only fire off the emails when the transaction is successful.

DB::beginTransaction();

try {
    event(new LaravelQuestionPosted($question));
    // Listeners: AddQuestionToQuestionsTable
    // Listeners: QuestionAddedNotificationSend
} catch (\Exception $e) {
    DB::rollback();
    // something went wrong
}

DB::commit();

Anyone know how to make it work as intended?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2nA180J
via IFTTT

Aucun commentaire:

Enregistrer un commentaire