Laravel: customizing or extending notifications - database model

IMHO, the current database feed for persisting notifications in Laravel is really bad design:

  • You cannot use foreign key cascades on items to clear deleted item notifications like
  • Finding custom attributes in a column data

    (cast from Array) is not optimal

How would you like to extend the model DatabaseNotification

in the vendor package?

I would like to add columns event_id

, question_id

, user_id

(user who created the notification), etc. to the default larvel tablenotifications

How do you override the function send

to add more columns?

IN:

vendor/laravel/framework/src/Illuminate/Notifications/Channels/DatabaseChannel.php

      

Code:

class DatabaseChannel
{
 /**
  * Send the given notification.
  *
  * @param  mixed  $notifiable
  * @param  \Illuminate\Notifications\Notification  $notification
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function send($notifiable, Notification $notification)
 {
    return $notifiable->routeNotificationFor('database')->create([
        'id' => $notification->id,
        'type' => get_class($notification),

      \\I want to add these
        'user_id' => \Auth::user()->id,
        'event_id' => $notification->type =='event' ? $notification->id : null, 
        'question_id' => $notification->type =='question' ? $notification->id : null,
      \\End adding new columns

        'data' => $this->getData($notifiable, $notification),
        'read_at' => null,
    ]);
 }
}

      

+8


source to share


5 answers


To create your own notification channel:

First create a class in App \ Notifications, for example:

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;

class CustomDbChannel 
{

  public function send($notifiable, Notification $notification)
  {
    $data = $notification->toDatabase($notifiable);

    return $notifiable->routeNotificationFor('database')->create([
        'id' => $notification->id,

        //customize here
        'answer_id' => $data['answer_id'], //<-- comes from toDatabase() Method below
        'user_id'=> \Auth::user()->id,

        'type' => get_class($notification),
        'data' => $data,
        'read_at' => null,
    ]);
  }

}

      



Second, use this channel in your via

Notification class method :

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;

use App\Notifications\CustomDbChannel;

class NewAnswerPosted extends Notification
{
  private $answer;

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

  public function via($notifiable)
  {
    return [CustomDbChannel::class]; //<-- important custom Channel defined here
  }

  public function toDatabase($notifiable)
  {
    return [
      'type' => 'some data',
      'title' => 'other data',
      'url' => 'other data',
      'answer_id' => $this->answer->id //<-- send the id here
    ];
  }
}

      

+18


source


Create and use your own model Notification

and Notifiable

then use your own Notifiable function in your (custom) models.

App \ Notifiable.php:

namespace App;

use Illuminate\Notifications\Notifiable as BaseNotifiable;

trait Notifiable
{
    use BaseNotifiable;

    /**
     * Get the entity notifications.
     */
    public function notifications()
    {
        return $this->morphMany(Notification::class, 'notifiable')
                            ->orderBy('created_at', 'desc');
    }
}

      

App \ Notification.php:



namespace App;

use Illuminate\Notifications\DatabaseNotification;

class Notification extends DatabaseNotification
{
    // ...
}

      

App \ User.php:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    // ...
}

      

+3


source


Unlike Bassem El Hachem, I wanted to keep the keyword database

in the methods via()

.

So in addition to the custom one DatabaseChannel

I also wrote my own ChannelManager

which returns my own DatabaseChannel

in createDatabaseDriver()

.

In ServiceProvider::register()

my applications method , I rewrote the singleton for the original ChannelManager class to return my own manager.

+3


source


Sample @ cweiske answer .

If you really need to expand Illuminate\Notifications\Channels\DatabaseChannel

without creating a new channel, you can:

Expands the channel:

<?php

namespace App\Notifications;

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

class MyDatabaseChannel extends BaseDatabaseChannel
{
    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function send($notifiable, Notification $notification)
    {
        $adminNotificationId = null;
        if (method_exists($notification, 'getAdminNotificationId')) {
            $adminNotificationId = $notification->getAdminNotificationId();
        }

        return $notifiable->routeNotificationFor('database')->create([
            'id' => $notification->id,
            'type' => get_class($notification),
            'data' => $this->getData($notifiable, $notification),

            // ** New custom field **
            'admin_notification_id' => $adminNotificationId,

            'read_at' => null,
        ]);
    }
}


      

And register again Illuminate\Notifications\Channels\DatabaseChannel

in the application container:

app\Providers\AppServiceProvider.php

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            Illuminate\Notifications\Channels\DatabaseChannel::class,
            App\Notifications\MyDatabaseChannel::class
        );
    }
}


      

Now when try Illuminate\Notifications\ChannelManager

createDatabaseDriver

will return your registered database driver.

Another solution to this problem!

+1


source


In my opinion laravel notifications are not ready and with very limited functionality.

Anyway, I solved a similar problem by customizing the notification class:

create a class for this action:

artisan make:notification NewQuestion

      

inside it:

public function __construct($user,$question)
    {
        $this->user=$user;
        $this->question=$question;
    }


...

    public function toDatabase($notifiable){
        $data=[
            'question'=>$this->(array)$this->question->getAttributes(),
            'user'=>$this->(array)$this->user->getAttributes()
        ];

        return $data;
    }

      

then you can access the relevant data in the view or controller like this:

@if($notification->type=='App\Notifications\UserRegistered')
<a href="{!!route('question.show',$notification->data['question']['id'])!!}">New question from {{$notification->data['user']['name']}}</a>
@endif

      

0


source







All Articles