viicslen/laravel-alertable

This is my package laravel-alertable

v1.1.0 2024-10-24 13:06 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is a simple package to add alerts to your laravel models.

Installation

You can install the package via composer:

composer require viicslen/laravel-alertable

You can publish and run the migrations with:

php artisan vendor:publish --tag="alertable-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="alertable-config"

This is the contents of the published config file:

return [
    /**
     * The default model to use for alerts.
     */
    'model' => ViicSlen\LaravelAlertable\Models\Alert::class,

    /**
     * Connection settings to use for the default alerts model.
     */
    'database' => [
        /**
         * The connection to use. If left nulls, the default connection will be used.
         */
        'connection' => null,

        /**
         * The table to use. If left nulls, the default connection will be used.
         */
        'table' => null,
    ]
];

Usage

First define any model as alertable by adding the HasAlerts trait to your model (and optionally the Alertable interface):

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use ViicSlen\LaravelAlertable\Concerns\HasAlerts;
use ViicSlen\LaravelAlertable\Contracts\Alertable;

class User extends Authenticatable implements Alertable
{
    use HasAlerts;
}

Create an alert:

use ViicSlen\LaravelAlertable\Enums\Severity;

// ...

$user = Auth::user();

$user->newAlert('This is a test alert', ['extra' => 'data'], Severity::Success);

Retrieve alerts:

$user = Auth::user();

// Get all alerts
$user->alerts()->get();

// Get alerts with a specific severity
$user->infoAlerts()->get();
$user->successAlerts()->get();
$user->warningAlerts()->get();
$user->errorAlerts()->get();

// Get latest alert
$user->latestAlert

Delete alerts:

use ViicSlen\LaravelAlertable\Enums\Severity;

// ...

// Delete all alerts
$user->clearAlerts()

// Delete alerts with a specific severity
$user->clearAlerts(Severity::Success)

// Delete alerts with a specific severity and category
$user->clearAlerts(Severity::Success, 'CATEGORY_1')

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.