A package for injecting variables into models after they were retrieved from the database.
You can install the package via composer:
composer require creativeorange/laravel-injectable
For Laravel 8 and lower you should require version 1.
If you are using Spaties Laravel Translatable this package needs an extra plugin, read instructions in: Laravel Injectable translatable extension
In this example we make use of a Question
model. This model has two attributes: name
and description
.
Under the hood, we use Laravel's translation system and helper method __()
. Therefor we need variables to be stored as :variable_name
. In this readme, we use the variable :company_name
.
First, make sure the $casts
attribute on the model is filled:
protected $casts = [
'name' => \Creativeorange\LaravelInjectable\Casts\InjectableCast::class,
'description' => \Creativeorange\LaravelInjectable\Casts\InjectableCast::class
];
After this, we can replace variables in the from the database retrieved attributes.
$question = \App\Models\Question::first();
$question->name->inject('company_name', 'Acme Inc.');
// or to use multiple:
$question->name->inject(['company_name', 'Acme Inc.', 'another_var' => 'Another val']);
echo $question->name; // will output the name with the variables replaced
echo $question->description; // will output the original text, without any replacements; for example: This is the description for company :company_name
As shown here, only the name
attributes will contain replacements, the description
still shows the original text from the database.
For this, the model needs to use the App\InjectableTrait
trait.
After this, a inject
method is available on the model.
$question = \App\Models\Question::first();
$question->inject('company_name', 'Acme Inc.');
// or to use multiple:
$question->inject(['company_name', 'Acme Inc.', 'another_var' => 'Another val']);
echo $question->name; // will output the name with the variables replaced
echo $question->description; // will output the description with the variables replaced
In this example, all variables will be replaced in the fields that were casted by the App\Casts\InjectableCast
.
Another use case is that you want to replace all the variables on request-level.
// Use the alias
\LaravelInjectable::set('company_name', 'Acme Inc.');
// Or directly use the facade
\CreativeOrange\LaravelInjectable\Facades\LaravelInjectable::set('company_name', 'Acme Inc.');
// Or via the app method
app(\Creativeorange\LaravelInjectable\LaravelInjectable::class)->set('company_name', 'Acme Inc.');
// Or set multiple vars at once
\LaravelInjectable::set(['company_name', 'Acme Inc.', 'another_var' => 'Another val']);
\LaravelInjectable::set('company_name', 'Acme Inc.')->set('another_var', 'Another val');
$question = \App\Models\Question::first();
echo $question->name; // will output the name with the variables replaced
echo $question->description; // will output the description with the variables replaced
Same as above, but then in blade
@inj('company_name', 'Acme Inc.')
//or to use multiple
@inj(['company_name', 'Acme Inc.', 'another_var' => 'Another val'])
@php($question = \App\Models\Question::first());
{{ $question->name }} {{-- will output the name with the variables replaced --}}
{{ $question->description}} {{-- will output the description with the variables replaced --}}
composer test
Please see CHANGELOG for more information what has changed recently.
The MIT License (MIT). Please see License File for more information.