rezawikan / custom-notifications
Custom notifications for Laravel
This package is auto-updated.
Last update: 2024-09-10 14:32:05 UTC
README
Package for custom notification (DatabaseChannels) in Laravel.
This package help you to improve laravel notification in various types of model and class. Let's see how easily to setup.
Installation
You can install the package via composer:
composer require rezawikan/custom-notifications
Usage
The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
'providers' => [ // ... Rezawikan\CustomNotifications\CustomNotificationServiceProvider::class, ];
Migrate the database and create your first notification for use this package.
php artisan migrate
php artisan make:notification NotificationName
Use the custom database channel in notification. If you want to add additional method such us mail or SMS it's fine.
use Rezawikan\CustomNotifications\Notifications\Channels\DatabaseChannel; /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [ DatabaseChannel::class ]; }
Customize your representation array of the notification. You can add anything, according your data that your recieve on the construct method.
- note that if you changes
toArray
function with another structure. You can restructure on database withphp artisan notifications:restructure
/** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ 'comment' => [ 'id' => $this->comment->id, 'body' => $this->comment->body ] ]; }
Now, move to user model. You must the the relation to the notification table.
use Illuminate\Notifications\Notifiable; use App\App\Notifications\Models\DatabaseNotification; class User extends Authenticatable { use Notifiable; /** * [posts description] * @return [type] [description] */ public function notifications() { return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderBy('created_at','desc'); } }
Then you can run and use it in your controller or routes.
$user = User::find(1); $comment = Comment::find(1); $user->notify(new CommentCreated($comment));
Attribute
There are attribute value on the $notification object. In this package, we have additional value.
$notification->models $notification->type_class
Tips on the blade view
Generate notification in controller. Make sure you have login to your app.
$notifications = $request->user()->notifications;
Create slot blade, put on the resources/notifications/notification.blade.php
<div> {{ $slot }} Mark as read </div>
Create component blade, put on the resources/notifications/types/name.blade.php Name of file, must according to model name. In our example, we use model comment, you can add with other model.
@component('notifications/notification') {{ $notification->data->author->name }} posted comment @endcomponent
On the index blade or that want to display the notifications list.
@foreach($notifications as $notification) @include('notifications/type/'. snake_case($notification->type), compact('notification')) @endforeach
Meta
Mochammad Rezza Wikandito – @rezawikan – reza.wikan.dito@gmail.com