Skip to content

[5.6] Add ability to extend DatabaseNotification model attributes on create#23337

Merged
taylorotwell merged 3 commits into
laravel:5.6from
cybercog:feature/extend-database-notification-model-payload
Feb 28, 2018
Merged

[5.6] Add ability to extend DatabaseNotification model attributes on create#23337
taylorotwell merged 3 commits into
laravel:5.6from
cybercog:feature/extend-database-notification-model-payload

Conversation

@antonkomarev

@antonkomarev antonkomarev commented Feb 28, 2018

Copy link
Copy Markdown
Contributor

Building of attributes array for the Illuminate\Notifications\DatabaseNotification model inside Illuminate\Notifications\Channels\DatabaseChannel extracted to method, what allow us to extend or overwrite default attributes.

I found it useful because I were needed to add seen_at and some more attributes related to my application and were needed to extend default DatabaseNotification model. And instead just modifying attributes in DatabaseChannel class on notification creation, we need to copy-paste send method. That means that if there will be changes in send method in the future - we will require to update extended implementations.

With this change we can do:

<?php

namespace App\Notification\Channels;

use Illuminate\Notifications\Channels\DatabaseChannel as BaseDatabaseChannel;
use Illuminate\Notifications\Notification;

class DatabaseChannel extends BaseDatabaseChannel
{
    protected function buildPayload($notifiable, Notification $notification)
    {
        return array_merge(parent::buildPayload($notifiable, $notification), [
            'seen_at' => null,
            'after_updrade' => 'Sleep well',
        ];
    }
}

Additionally it helps us to keep the code DRY.

@taylorotwell
taylorotwell merged commit 4380ac8 into laravel:5.6 Feb 28, 2018
@antonkomarev
antonkomarev deleted the feature/extend-database-notification-model-payload branch February 28, 2018 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants