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
Requires
- php: ^8.1
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
README
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.