eighteen73 / laravel-tokens
A simple package for managing Laravel token replacement from model data
Fund package maintenance!
eighteen73
Installs: 38
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/eighteen73/laravel-tokens
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
You can use provides an automated way to replace tokens in user-entered text with model/relation data. This is useful for things like email templates.
All unguarded model attributes are available as tokens - as well as relation data accessed through dot notation.
When model factories are available, the factory definition attributes will also available as tokens, otherwise Model::getAttributes() will be used.
Installation
You can install the package via composer:
composer require eighteen73/laravel-tokens
Usage
List available tokens
$tokenManager = new Eighteen73\LaravelTokens\TokenManager(); echo $tokenManager->forModel(App\Models\User::class)->plainTokens();
Replace tokens in a string
$tokenManager = new Eighteen73\LaravelTokens\TokenManager(); echo $tokenManager->forModel(User::factory()->make(['email' => 'test@example.com'])) ->replaceTokens("My email address is ##email##."); // My email address is test@example.com
Custom tokens within a model
use Eighteen73\LaravelTokens\Contracts\CustomTokens; class User extends Model implements CustomTokens { public function getCustomTokens(): array { return [ 'my_custom_token' => 'Use this custom text.', ]; } } $tokenManager = new Eighteen73\LaravelTokens\TokenManager(); echo $tokenManager->forModel(User::factory()->make()) ->replaceTokens("Example text - ##my_custom_token##."); // Example text - Use this custom text.
Testing
composer test
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
License
The MIT License (MIT). Please see License File for more information.