abather/model-notification

Add notifications Template into Models

Fund package maintenance!
Mohammed Sadiq

Installs: 10

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Open Issues: 1

pkg:composer/abather/model-notification

1.0.0 2024-04-25 06:42 UTC

This package is auto-updated.

Last update: 2026-02-17 06:15:58 UTC


README

Latest Version on Packagist Total Downloads License

Stop hardcoding notification strings. Model Notification gives you a powerful, database-driven way to manage dynamic notification templates for your Eloquent models.

Supports variable replacement, multi-language fallbacks, and file attachmentsโ€”all with a fluent, developer-friendly API.

โœจ Features

  • ๐Ÿ“ Dynamic Templates - Manage templates in your database, not your code.
  • ๐Ÿ”„ Smart Variables - Inject model attributes [id], relationships [user->name], and even method results [total()].
  • ๐ŸŒ Localization - Automatic language handling with fallback support.
  • ๐Ÿ“Ž Attachments - Easily include files (like invoices or reports) in your messages.
  • ๐Ÿ’พ Caching & Performance - Built-in caching for high-speed retrieval.
  • โœ… Validation - Catch syntax errors before they reach your users.

๐Ÿš€ Quick Start

1. Install via Composer

composer require abather/model-notification

2. Publish Migrations

php artisan vendor:publish --tag="model-notification-migrations"
php artisan migrate

3. Prepare Your Model

Add the Notifier trait and interface to any model you want to send notifications for (e.g., Invoice, Order, User).

use Abather\ModelNotification\Contracts\Notifier;
use Abather\ModelNotification\Notifier as NotifierTrait;
use Illuminate\Database\Eloquent\Model;

class Invoice extends Model implements Notifier
{
    use NotifierTrait;
}

4. Create & Use a Template

// 1. Create a template (usually in a seeder or admin panel)
Invoice::makeTemplateMessage()
    ->key('invoice_paid')
    ->channel('email')
    ->lang('en')
    ->template('Hi [client->name], your payment of [formatted_amount] for invoice #[id] was received.')
    ->save();

// 2. Fetch the message for a specific invoice
$invoice = Invoice::find(1);
$message = $invoice->getTemplateMessageText('invoice_paid', 'en', 'email');

// Output: "Hi John Doe, your payment of $150.00 for invoice #1001 was received."

๐Ÿ“š Variable Syntax

The real power lies in how you can use variables in your templates.

Type Syntax Description Example
Attribute [column_name] Value of a model column Invoice #[id]
Relationship [relation->col] Value from a related model Thanks, [user->name]
Method [method()] Result of a method call Total: [calculateTotal()]
File [file_path] Link to an attached file Download: [file_path]

Note: You can customize the start ([) and end (]) delimiters in the config file.

๐Ÿ› ๏ธ Advanced Usage

Including Files

Need to send a PDF or image? Just chain includeFile() when creating the template.

Invoice::makeTemplateMessage()
    ->key('invoice_sent')
    ->template('Here is your invoice #[id].')
    ->includeFile() // <--- Enables file attachment
    ->save();

// Retrieve file URL later
$url = $invoice->getFile('invoice_sent', 'en', 'email');

Extra Data (Prob)

Sometimes you need dynamic data that isn't on the model, like a custom subject line or icon.

Invoice::makeTemplateMessage()
    ->key('push_notification')
    ->template('New Order Received')
    ->prob([
        'title'   => 'Order #[id]',
        'icon'    => 'cart',
        'deep_link' => 'app://orders/[id]'
    ])
    ->save();

// Retrieve parsed data
$data = $invoice->getTemplateMessageProb('push_notification', 'en', 'push');
// Result: ['title' => 'Order #1001', 'icon' => 'cart', 'deep_link' => 'app://orders/1001']

โš™๏ธ Configuration

Publish the config file to customize caching, validation rules, and strict mode settings.

php artisan vendor:publish --tag="model-notification-config"

Useful options:

  • fallback_lang: Default language if the requested one is missing (default: ar).
  • max_depth: Maximum depth for nested variable resolution (default: 10).
  • strict_mode: Throw exceptions if a variable is missing (default: false).
  • cache.ttl: How long to cache templates (default: 24 hours).

๐Ÿงช Testing

We use Pest for testing. Run the suite to ensure everything is working correctly.

composer test

๐Ÿ“„ License

The MIT License (MIT). See License File for more information.