Download the PHP package abather/model-notification without Composer
On this page you can find all versions of the php package abather/model-notification. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download abather/model-notification
More information about abather/model-notification
Files in abather/model-notification
Package model-notification
Short Description Add notifications Template into Models
License MIT
Homepage https://github.com/abather/model-notification
Informations about the package model-notification
Add notifications Template into Models
This package helps you organized and save template messages for each model that included it. each message depends
upon key
, language
, and channel
.
Installation
You can install the package via composer:
You can publish and run the migrations with:
You can publish the config file with:
This is the contents of the published config file:
Override Global Configuration:
You can prevent including files for specific models if you wish by adding the variable prevent_including_file
in the model:
If you want to use specific file variables for the model, you can add the file_variables
variable:
Usage
You can use this package with any Model
by implementing the Notifier
interface and using the Notifier
trait:
Now you can create, call, or update message templates as described below.
Create new template for a model:
To create a new template for any model, you can use the makeTemplateMessage()
method. You must specify key
, lang
, channel
, and template
:
Including variables in the template text:
The template()
method can include any attribute present in the model. The attribute value will replace the attribute name. For example, [amount]
will retrieve the value of the attribute named amount
. To change the symbols representing the variable names, you can modify the variable_starter
and variable_ender
in the config file.
Including attributes from relationships:
The template()
method can also include values from supported relationships, such as [user->name]
. It behaves like including attributes and supports belongsTo
and hasOne
relationships.
Including file path in the template text:
You can include the file URL in the template by adding any key defined in the config("model-notification.file_variables")
or defined in the model $file_variables
. Additionally, you need to use includeFile()
when creating the template message:
You can change the URL returned by overriding the getFilePath()
method in your model.
Adding extra data for the template message:
You can pass any extra data to prob([$key => $value])
. Each value will go through the same process as the template to replace the attributes with data for attributes, relationship attributes, or file URLs.
Getting Template Messages:
You can use the getTemplateMessages()
method to get all messages related to the model:
If you want to get messages for a specific channel, language, or key, you can use the forChannel($channel)
, forLang($lang)
, or forKey($key)
scopes with the notificationTemplates()
query builder:
This will return a collection of NotificationTemplate
.
If you want to get a specific Template Message, you can use the getTemplateMessage
method by passing key
, lang
, and channel
:
This method will return a NotificationTemplate
instance.
If you want to get the text message, you can use the getTemplateMessageText()
method by passing key
, lang
, and channel
:
This will return a string ready to use with your notification, with all variables, relationship variables, and file paths replaced.
To get prob
for a specific template, you can use the getTemplateMessageProb()
method and pass key
, lang
, and channel
:
This will return an array with each prob
ready to use and handled. If you want to get the value of a specific prob
, you can pass a fourth parameter, prob
:
This will return a string of the value of the given prob
or an empty string if it does not exist.
If the file is included with the template, you can get the file URL or the file object by using the ()
method and passing key
, lang
, and channel
:
This will return the file URL. If you want to get the file parameter, you have to pass a fourth parameter as false
:
This will return a file parameter. Keep in mind that you have to configure the getFileObject()
method inside your model.
Testing
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
- Mohammed Sadiq
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of model-notification with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^10.0||^11.0