martin.fdm's avatar

How to persist Notifications changes on Database? Laravel (8) Notifications

Hi everybody ! I've read the Notifications documentation, but the Mark as Read part is so brief and manages unread Notifications as a whole and not individually. So, I'm using Eloquent to get the notification by id, is ok, right? Thing is, as you can see in the code, I've tried multiple ways, but unsuccessfully. The read_at value changes in the method but is not persisted in DB.

public function handler($notification_id) {
           
        try {

            DB::beginTransaction();

		    /* MARK as read not working even though next variable gets the correct notification (its datatype is DatabaseNotification)*/

            $notification = auth()->user()
            ->unreadNotifications
            ->where('id', $notification_id)->first();

  			$notification->markAsRead();            // 1st way

			auth()->user()->Notifications
                ->where('id', '=', $notification_id)
                ->markAsRead();                               // 2nd way

			$notification->update(
                ['read_at'=>Carbon::now(),				// 3rd way
            ]);

			$notification->save();							// even try this

	       dd($notification);						// shows read_at with correct datetime 

		    DB::commit(); 

		// and catch staff

What should I do ?

Thanks in advance!

0 likes
6 replies
Snapey's avatar

You will never get to the commit statement with your DD there

Why are you even using a transaction - pointless in this context

martin.fdm's avatar

@Snapey the dd() is there for pointing out that $notification is the right one.. is of course commented out when it's not needed. The transaction is pointless in the context? Didn't know.. ok

Please or to participate in this conversation.