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.5.0(1mo ago)38.0k—8.3%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)[ RSS](/packages/fomvasss-laravel-str-tokens/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (26)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)

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:

```
