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

1.0.1 2025-10-17 13:05 UTC

This package is auto-updated.

Last update: 2025-10-17 13:07:48 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.