Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Feb 28, 2018
Merged

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

merged 3 commits into from
Feb 28, 2018

Conversation

antonkomarev
Copy link
Contributor

@antonkomarev antonkomarev commented Feb 28, 2018

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