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
此包帮助您为包含每个模型的模板消息进行组织和保存。每条消息都依赖于key
、language
和channel
。
安装
您可以通过composer安装此包
composer require abather/model-notification
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="model-notification-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="model-notification-config"
这是已发布配置文件的内容
return [ "fallback_lang" => env("MODEL_NOTIFICATION_FALLBACK_LANG", "ar"), "variable_starter" => "[", "variable_ender" => "]", "relationship_variable_symbol" => "->", "file_name" => "file", "prevent_including_file" => false, "file_variables" => [ "file", "file_path", "attachment" ] ];
覆盖全局配置
如果您希望排除特定模型的文件,可以在模型中添加变量prevent_including_file
public static $prevent_including_file = true;
如果您想为模型使用特定的文件变量,可以添加变量file_variables
public static $file_variables = ["document"];
使用方法
您可以通过实现Notifier
接口和使用Notifier
特质,使用此包与任何Model
一起使用
namespace App\Models; use Abather\ModelNotification\Contracts\Notifier; use Illuminate\Database\Eloquent\Model; class Bill extends Model implements Notifier { use \Abather\ModelNotification\Notifier; }
现在您可以创建、调用或更新如下所述的消息模板。
为模型创建新模板
要为任何模型创建新模板,您可以使用makeTemplateMessage()
方法。您必须指定key
、lang
、channel
和template
Bill::makeTemplateMessage() ->key("new") ->channel("sms") ->lang("ar") ->template("You have a new bill [id], with an amount of: [amount]") ->save();
在模板文本中包含变量
template()
方法可以包含模型中存在的任何属性。属性值将替换属性名称。例如,[amount]
将检索名为amount
的属性值。要更改表示变量名称的符号,您可以修改配置文件中的variable_starter
和variable_ender
。
包含关系中的属性
template()
方法还可以包含支持的关系中的值,例如[user->name]
。它类似于包含属性,并支持belongsTo
和hasOne
关系。
在模板文本中包含文件路径
您可以通过添加在config("model-notification.file_variables")
中定义的任何键或在模型中定义的$file_variables
来在模板中包含文件URL。此外,在创建模板消息时,您还需要使用includeFile()
。
Bill::makeTemplateMessage() ->includeFile();
您可以通过在模型中覆盖getFilePath()
方法来更改返回的URL。
为模板消息添加额外数据
您可以将任何额外数据传递给prob([$key => $value])
。每个值都将与模板相同的处理过程替换属性、关系属性或文件URL。
Bill::makeTemplateMessage() ->prob(["title" => "New bill generated with number [id]", "icon" => "bill"]);
获取模板消息
您可以使用getTemplateMessages()
方法获取与模型相关的所有消息
Bill::getTemplateMessages();
如果您想获取特定通道、语言或键的消息,您可以使用notificationTemplates()
查询构建器的forChannel($channel)
、forLang($lang)
或forKey($key)
作用域
Bill::notificationTemplates() ->forChannel("sms") ->forLang("ar") ->get();
这将返回一个NotificationTemplate
集合。
如果您想获取特定的模板消息,可以使用通过传递key
、lang
和channel
的getTemplateMessage
方法
Bill::getTemplateMessage("anyKey", "ar", "sms");
此方法将返回一个NotificationTemplate
实例。
如果您想获取文本消息,可以使用通过传递key
、lang
和channel
的getTemplateMessageText()
方法
Bill::getTemplateMessageText("anyKey", "ar", "sms");
这将返回一个字符串,该字符串准备好与您的通知一起使用,其中已替换所有变量、关系变量和文件路径。
要获取特定模板的 prob
,您可以使用 getTemplateMessageProb()
方法并传递 key
、lang
和 channel
Bill::getTemplateMessageProb("anyKey", "ar", "sms");
这将返回一个数组,其中包含每个 prob
已准备好使用和处理。如果您想获取特定 prob
的值,您可以传递一个第四个参数,即 prob
Bill::getTemplateMessageProb("anyKey", "ar", "sms", "title");
这将返回给定 prob
的值字符串,如果不存在,则返回空字符串。
如果文件与模板一起包含,您可以使用 ()
方法获取文件 URL 或文件对象,并传递 key
、lang
和 channel
Bill::getFile("anyKey", "ar", "sms");
这将返回文件 URL。如果您想获取文件参数,必须将第四个参数传递为 false
Bill::getFile("anyKey", "ar", "sms", false);
这将返回文件参数。请注意,您必须在自己的模型中配置 getFileObject()
方法。
测试
composer test
变更日志
请参阅 变更日志 了解最近有哪些变化。
贡献
请参阅 贡献指南 了解详细信息。
安全漏洞
请查阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。