michabbb's avatar

need advise: eloquent notifications but inside transactions

hi all,

i really like the concept of sending notifications, when an object gets saved. BUT it seems like, it does not work, when your notification contains data from another table as well. example: someone is buying something, you save the order itself in "order" and the items in "order_item" - so when i send a notification when an order is saved via:

protected static function booted(): void {
        static::saved(static function ($order) {
            $order->notifyNow(new NewOrderCreated($order));
        });
    }

and inside my NewOrderCreated i am fetching items from "order_items" i don´t get anything. i guess because at the moment the notification gets sent, the "order_items" are not yet saved, because the whole saving of these two tables is happening inside a transaction.

i don´t know if i do something wrong here - but since i am using laravel, i learned: there is ALWAYS a solution for EVERYTHING ;-)))

maybe, in that case, i described, a object-notification is not useful - because of the transaction - may be there exists something, to make this working.

i would be very happy, to get some advice here.

thanks a lot! micha

0 likes
2 replies
michabbb's avatar

okay, turned out mvanduijker/laravel-transactional-model-events is perfect for exactly this need ! and if you want to build something on your own: you can register own events and eloquent is firering eloquent.afterCommit.* events.....

Please or to participate in this conversation.