abather/model-notification
Composer 安装命令:
composer require abather/model-notification
包简介
Add notifications Template into Models
README 文档
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.
abather/model-notification 适用场景与选型建议
abather/model-notification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Mohammed Sadiq」 「model-notification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 abather/model-notification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 abather/model-notification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 abather/model-notification 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easily manage uploaded files in your Laravel app, with automatic path saving and handy value objects for effortless access.
Alfabank REST API integration
Laravel package for counting model visits
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
统计信息
- 总下载量: 48
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-25