ccoeder
2 months ago
144
10
Laravel

Adding multiple notification channels to the user model.

Posted 2 months ago by ccoeder

Hey,

I'm trying to build a system that allows user's to add multiple notification channels, such as they could add their accountant's email address and also their slack webhook and one more additional webhook url like their api or something. There is no limit.

So I've designed a table, named as user_notification_channels. When the user signs up, I add his/her email address to this table.

Schema::create('user_notification_channels', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('name');
            $table->tinyInteger('type');
            $table->string('url');

            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');
        });

And this is my code on the event listener that sends the notification to the user and his/her additional notification channels.

        $user = $event->model->user;
        $user->notificationChannels()->each(function ($item, $key) {
            $type = NotificationChannels::getDescription($item->type);

            Notification::route(strtolower($type), $item->url)->notify(new MyNotification());
        });

But it's not working properly, first of all, I don't want to iterate all of the notification channels that user has, is there a proper way to do it?

And second, what should I do for adding multiple webhook url's or slack url's?

Thanks for the help.

Please sign in or create an account to participate in this conversation.