Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Moenchfracht's avatar

Notification and broadcasts

Hello, I need some help to understand the basics of notifications together with broadcasts. I am allready using broadcasts with pusher in my application. Now I want to save these broadcasts in the database. I want that the users see their missed broadcasts when they log in again. I am sure I need notifications for that. But I don't know where to start. I allready have my event which broadcasts a message with pusher. What's next? Thanks! :)

0 likes
12 replies
hfalucas's avatar

In your notification class add database to in the via method:

    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }

   public function toDatabase($notifiable)
    {
        return [
            // data to be saved in the db
        ];
    }

    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            // data to broadcast
        ]);
    }
Moenchfracht's avatar

OK. Now I'm broadcasting with my

            event(new \App\Events\NewDeliverynote($dn->id));

Should I change this to

$user->notify(new ...($dn->id));

or do I need both?

And how can I see which notifications a user has missed while he was logged out?

hfalucas's avatar

This is how I am doing:

1) Add the Notifiable trait in the User model

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use  Notifiable;
    // ...
}

2) Fire the Notification

Notification::send($users, new PostWasCreated($post));

3) Choose the channels

class PostWasCreated extends Notification implements ShouldQueue
{
    use Queueable;


    protected $post;


    public function __construct( $post)
    {
        $this->post = $post;
    }


    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }


    public function toDatabase($notifiable)
    {
        return [
        'id' => $post->id,
        'title' => $post->title
        ];
    }


    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
        'id' => $post->id,
        'title' => $post->title
    ]);
    }
}

4) Fetch the unread notifications for the user

$user->unreadNotifications()
Moenchfracht's avatar

@HFALUCAS - Thanks for that. One more question: where should I fire the notification from? And should I also fire the broadcasting-event? If so, which one first?

hfalucas's avatar

One more question: where should I fire the notification from?

I don't know if there's a right place for that, but from the controller doesn't seem to be bad.

And should I also fire the broadcasting-event? If so, which one first?

Yes broadcast the event and then the notification or vice-versa :)

Moenchfracht's avatar

@HFALUCAS - Its working. Only strange thing is that when I send the notification to more than one user, the entries in the database have a wrong id stored. It's allways the id from the notification and not my data. :(

Moenchfracht's avatar

:) My notification table looks like this:

mysql> select * from notifications order by 'created_at' desc limit 2;
+--------------------------------------+-------------------------------------+-----------------+---------------+-----------------------------------------------+---------+---------------------+---------------------+
| id                                   | type                                | notifiable_type | notifiable_id | data                                          | read_at | created_at          | updated_at          |
+--------------------------------------+-------------------------------------+-----------------+---------------+-----------------------------------------------+---------+---------------------+---------------------+
| 0f1cadca-46d6-4fbd-a779-6fee5ca540f1 | App\Notifications\DeliverynoteSaved | App\User        |             3 | {"id":66}                                     | NULL    | 2019-01-11 09:01:51 | 2019-01-11 09:01:51 |
| 0f317d84-5069-44a4-91cd-88cbfcb677f6 | App\Notifications\DeliverynoteSaved | App\User        |             4 | {"id":"0f317d84-5069-44a4-91cd-88cbfcb677f6"} | NULL    | 2019-01-11 12:41:27 | 2019-01-11 12:41:27 |
+--------------------------------------+-------------------------------------+-----------------+---------------+-----------------------------------------------+---------+---------------------+---------------------+
2 rows in set (0,00 sec)

User '4' should also have data id:66, but it has the id from the table

hfalucas's avatar

Can you show the DeliverynoteSaved class and where it is called?

Moenchfracht's avatar

@HFALUCAS -

class DeliverynoteSaved extends Notification implements ShouldQueue
{
    use Queueable;

    public $deliverynote;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($id)
    {
        $this->id = $id;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the database representation of the notification.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'id' => $this->id
        ];
    }

I call it from the Event using

        \Notification::send($users, new \App\Notifications\DeliverynoteSaved($this->id));

I think thats the mistake....

hfalucas's avatar

The class seems fine.

You are firing the notification inside the event listener? And inside there you also have the collection of users(or user) to send the notification to?

Moenchfracht's avatar

@HFALUCAS - Yes, I changed it to fire it from the Controller... but the database entries are the same :/

Just found it :)

    public function __construct($dnid)
    {
        $this->dnid = $dnid;
    }

Had to change id to dnid (or whatever). Thanks for your help!!!

2 likes

Please or to participate in this conversation.