Skip to content

pestopancake/laravel-backpack-database-notifications

Repository files navigation

Laravel Backpack Database Notifications

Packagist Version Build Status StyleCI

Easily add an admin interface for standard Laravel database notifications. This package includes:

  • a sidebar item, with an optional notification count for the current user (refreshed with AJAX every second)
  • an interface that shows the notifications in the database for the current user, with their (optional) action buttons;

The only thing left for you to do is to actually trigger notifications for your admins, wherever you want, using the standard Laravel syntax (example below).

Prerequisites

php artisan notifications:table
php artisan migrate

Installation

Backpack v5 / v6

composer require pestopancake/laravel-backpack-database-notifications

Backpack v4

composer require pestopancake/laravel-backpack-database-notifications:1.0.6

Usage

Publish the config file:

php artisan vendor:publish --provider="Pestopancake\\LaravelBackpackNotifications\\LaravelBackpackNotificationsServiceProvider" --tag=config

Show in side menu

Add a menu item to your 'resources/views/vendor/backpack/base/inc/sidebar_content.blade.php' by running:

php artisan backpack:add-menu-content "@include('backpack-database-notifications::sidebarMenuItem')"

Admin view

With the Permission Manager package installed you can assign the permission 'notifications admin' to users for them to see admin functionality.

To change the permission name edit 'admin_permission_name' in the databasenotifications config file.

Currently users with the admin permission can see/dismiss notifications for all users.

Create a notification

Use the included generic notification

use Pestopancake\LaravelBackpackNotifications\Notifications\DatabaseNotification;

$user = backpack_user();
$user->notify(new DatabaseNotification(
    $type = 'info', // info / success / warning / error
    $message = 'Test Notification',
    $messageLong = 'This is a longer message for the test notification '.rand(1, 99999), // optional
    $href = '/some-custom-url', // optional, e.g. backpack_url('/example')
    $hrefText = 'Go to custom URL' // optional
));

Use in any other notification

The toArray method of the notification should be in this format:

return [
    'type' => "info", // info / success / warning / error
    'message' => "",
    'message_long' => "", // optional
    'action_href' => "", // optional, e.g. backpack_url('/example')
    'action_text' => "", // optional
];

The type will affect the colour of the toast notification (if toasts are enabled in the config)

Troubleshooting

Notification not created

  • Make sure the model you are notifying matches the model defined in your backpack config at backpack.base.user_model_fqn (found in config/backpack/base.php)

Change log

See the changelog for more information on what has changed recently.

Todo

  • Add unit tests
  • translatable text
  • notification preferences

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published