tadasei/backend-trashable-notifications

A package that provides stubs for soft deletable database notifications support

v1.1.3 2024-12-06 18:48 UTC

This package is auto-updated.

Last update: 2025-04-06 19:31:12 UTC


README

This package provides stubs for managing trashable (soft deletable) database notifications in the backend of a Laravel application. It simplifies common Index, Store, Update, and Delete notification operations by providing pre-defined structures.

Features

  • Quickly generate trashable database notifications management files and handling logic.
  • Customize and extend generated code to fit your project's needs.
  • Improve development efficiency by eliminating repetitive tasks.

Installation

Install the package via Composer by running:

composer require tadasei/backend-trashable-notifications --dev

Usage

Publishing Trashable Notifications Management Utilities

To publish the utilities, including the supervisord configuration files, run:

php artisan trashable-notifications:install

This command generates all necessary files, including:

  • Notification-related form requests.
  • Policies for managing trashable notifications.
  • A custom Notifiable trait.
  • Route files for notification management.
  • Database migration files.
  • Supervisord configuration files for production queue management.

Configuration

After publishing the utilities, follow these steps to complete the configuration:

  1. Form Request Configuration: Modify the generated form request (App\Http\Requests\SendNotificationRequest) to suit your application's validation rules and logic.

  2. Policy Configuration: Update the generated policy (App\Policies\DatabaseNotificationPolicy) to control access to notification management operations, ensuring it aligns with your project's authorization system.

  3. Notifiable Trait Replacement: The package provides a custom Notifiable trait, which will be published along with the other management utilities. You must replace the built-in Laravel Notifiable trait with this one in your desired notifiable models. For example:

    use App\Traits\Notifiable;

    This ensures that your models properly handle trashable notifications.

  4. Routes Registration: The routes/resources/notification.php file is generated as part of the management utilities. You must register this file in your application's built-in routes/web.php or routes/api.php to make the package routes available. For example, in routes/api.php:

    <?php
    
    require __DIR__ . '/resources/notification.php';

    The generated routes are protected by the auth:sanctum middleware by default. If your project uses a different authentication guard, you may need to update the middleware to fit your authentication system.

    Route::middleware('auth:your_guard')->group(function () {
        // Register routes
    });
  5. Running the Migration: The package generates a migration as part of the utilities. You need to run this migration to set up the necessary database structure for managing trashable notifications. Use the following Artisan command:

    php artisan migrate

These steps are necessary to ensure that the package integrates smoothly with your application's existing structure.

Using Supervisord Configuration Files in Production

The supervisord configuration files, generated by the php artisan trashable-notifications:install command, are essential for managing queues in production.

  1. Copy the Configuration Files:
    Move the generated .conf files to the /etc/supervisor/conf.d directory in your production server. This allows supervisord to pick up the configuration and manage your processes as defined in the files.

  2. Learn More About Supervisor:
    For further details on how to configure and manage Laravel queues with Supervisor, refer to the Laravel documentation on Supervisor configuration.

Managing Log Files in Production

When using the package in production, log files generated by the supervisord configuration files must be excluded from source control to prevent unnecessary clutter in your versioning system.

Each generated supervisord configuration file has a name format:

<app_name_in_lower_snake>_<file_name>.conf

To exclude the associated log files, perform the following steps:

  1. Transform the Configuration File Name:
    Remove the <app_name_in_lower_snake>_ prefix and replace the .conf extension with .log, adding an asterisk (*) at the end. For example:

    • If the configuration file is my_app_worker.conf, the corresponding log file would be worker*.log.
  2. Update Your .gitignore:
    Add the transformed file name to your .gitignore file. For the above example, your .gitignore entry would look like this:

    worker*.log

This ensures that log files generated in production do not get included in your source control repository.

Further Customization

The generated code serves as a starting point. You can further extend and customize it to fit your project’s needs.

Contributing

Contributions are welcome! If you have suggestions, bug reports, or feature requests, please open an issue on the GitHub repository.

License

This package is open-source software licensed under the MIT license.