PHPackages                             fomvasss/laravel-str-tokens - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fomvasss/laravel-str-tokens

ActiveComposer-package[Utility &amp; Helpers](/categories/utility)

fomvasss/laravel-str-tokens
===========================

A package to manage and generate string with tokens/shortcodes for Eloquent Models

2.7.0(1mo ago)48.6k—1.7%1MITPHPPHP &gt;=7.0.0

Since Oct 26Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/fomvasss/laravel-str-tokens)[ Packagist](https://packagist.org/packages/fomvasss/laravel-str-tokens)[ Docs](https://github.com/fomvasss/laravel-str-tokens)[ Fund](https://send.monobank.ua/jar/5xsqtHvVrY)[ Fund](https://ko-fi.com/fomvasss)[ RSS](/packages/fomvasss-laravel-str-tokens/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (8)Versions (28)Used By (0)

Laravel Str Tokens
==================

[](#laravel-str-tokens)

[![License](https://camo.githubusercontent.com/b7460ea02a2996da663c0a331dc2f6c94c428868566a77321c64da806aa41569/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f666f6d76617373732f6c61726176656c2d7374722d746f6b656e732e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/fomvasss/laravel-str-tokens)[![Build Status](https://camo.githubusercontent.com/43b2a27f24e65138814f5cd541710ab3724eb45d606865f3176c931b19b5eed8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f666f6d76617373732f6c61726176656c2d7374722d746f6b656e732e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/fomvasss/laravel-str-tokens)[![Latest Stable Version](https://camo.githubusercontent.com/f170f3847c099b1778b83e52b25979af865f4ac3c2405a3a1bdc33edb8a8bb7b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f6d76617373732f6c61726176656c2d7374722d746f6b656e732e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/fomvasss/laravel-str-tokens)[![Total Downloads](https://camo.githubusercontent.com/d91dea68877a0129bfbf5fee84a2ae21e1cdbdba12dce949b074c2d69f84664c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f6d76617373732f6c61726176656c2d7374722d746f6b656e732e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/fomvasss/laravel-str-tokens)[![Quality Score](https://camo.githubusercontent.com/3f576d4065265b25177c0dbd2abf6900f986e2035532aeba70cac11de7ec11a2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f666f6d76617373732f6c61726176656c2d7374722d746f6b656e732e7376673f7374796c653d666f722d7468652d6261646765)](https://scrutinizer-ci.com/g/fomvasss/laravel-str-tokens)

Support
-------

[](#support)

If this package is useful to you, consider supporting its development:

[![Monobank](https://camo.githubusercontent.com/f4ca1cf4fd0a99ddfed314cb8f65140d8e83e324bd7c1f8476c52fe4ee6b25fd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d4d6f6e6f62616e6b2d626c61636b)](https://send.monobank.ua/jar/5xsqtHvVrY)[![Ko-Fi](https://camo.githubusercontent.com/10764ec41c79959f08ccaf3e066c7a76c371961b1b1ff5b5f2a33aceef6e857a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d4b6f2d2d66692d4646354535423f6c6f676f3d6b6f2d6669266c6f676f436f6c6f723d7768697465)](https://ko-fi.com/fomvasss)[![USDT TRC20](https://camo.githubusercontent.com/214b4dc05bedda1c5129726b4450ef7f672ce6637865f8a8be83c03582bfdce8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d5553445425323054524332302d3236413137423f6c6f676f3d746574686572266c6f676f436f6c6f723d7768697465)](https://link.trustwallet.com/send?coin=195&address=THLgp6DxiAtbNHvgnKV56vk1L38UuUagKf&token_id=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t)

> USDT TRC20 address: `THLgp6DxiAtbNHvgnKV56vk1L38UuUagKf`

With this package you can manage &amp; generate strings with tokens/shortcodes, it seems like CMS Drupal.

---

Installation
------------

[](#installation)

Run from the command line:

```
composer require fomvasss/laravel-str-tokens
```

To publish the configs, run the following command:

```
php artisan vendor:publish --provider="Fomvasss\LaravelStrTokens\ServiceProvider"

```

Configuration file will be publish to `config/str-tokens.php`

Configuration
-------------

[](#configuration)

The configuration fill will allow you to control how tokens are parsed using `token_match_pattern` and `token_split_character`

You can decide if a token can traverse eloquent model relationships using `can_traverse_relations`

You can globally limit what model fields are allowed as tokens using `disable_model_tokens`

You can also limit what tokens are exposed via individual models by creating a `strTokenWhitelist` or `strTokenBlacklist` function that returns an array of valid patterns

For finesh prepare string - use `formatters` - methods an functions for modify string

Usage
-----

[](#usage)

```
$str = StrToken::setText('
            Example str with tokens for article: "[article:title] ([article:id])",
            Article created at date: [article:created_at],
            Author: [article:user:name]([article:user:id]).
            Article status: [article:txArticleStatus:name],
            Article root category: [article:txArticleCategories:root:name],
            User: [article:user:email], [article:user:city:country:title], [article:user:city:title].
            Generated token at: [config:app.name], [date:raw]
            [article:test:Hello]!!!
            Length: [var:length];
            Width: [var:width];
            Price: [var:price]
        ')
    ->setDate(\Carbon\Carbon::tomorrow())
    ->setEntity(\App\Model\Article::findOrFail(13))
    ->setVars(['length' => '2.2 m.', 'width' => '3.35 m.'])
    ->setVar('price', '$13')
    ->replace();
```

Given result:

```
 Example str with tokens for article: "Test article title(23)",
 Article created at date: 15.07.2018,
 Author: Taylor Otwell(1),
 Article status: published,
 Article root category: Programming,
 User: taylorotwell@gmail.com, AR, Little Rock.
 Generated token at: Laravel, 2018-10-27 00:00:00
 TEST TOKEN:Hello!!!
 Length: 2.2 m.;
 Width: 3.35 m.;
 Price: $13

```

Use method `setEntities()` for set many Eloquent models, for example:

```
