PHPackages                             creativeorange/laravel-injectable - 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. creativeorange/laravel-injectable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

creativeorange/laravel-injectable
=================================

Injecting variables into models after they were retrieved from the database.

v3.0.0(1y ago)218211MITPHPPHP &gt;=8.0.2

Since Oct 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/creativeorange/laravel-injectable)[ Packagist](https://packagist.org/packages/creativeorange/laravel-injectable)[ Docs](https://github.com/creativeorange/laravel-injectable)[ RSS](/packages/creativeorange-laravel-injectable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (7)Used By (1)

Laravel Injectable
==================

[](#laravel-injectable)

[![Total Downloads](https://camo.githubusercontent.com/0cc6818d713ad3f659fa6d52cabb90f5502f3b5af0526939182a009bfe1f586e/68747470733a2f2f706f7365722e707567782e6f72672f63726561746976656f72616e67652f6c61726176656c2d696e6a65637461626c652f642f746f74616c2e737667)](https://packagist.org/packages/creativeorange/laravel-injectable)[![Latest Stable Version](https://camo.githubusercontent.com/f55ba4ee63065ed223ff3674612b82202dd1c0e3dba260c506edeb59348f0b1e/68747470733a2f2f706f7365722e707567782e6f72672f63726561746976656f72616e67652f6c61726176656c2d696e6a65637461626c652f762f737461626c652e737667)](https://packagist.org/packages/creativeorange/laravel-injectable)[![License](https://camo.githubusercontent.com/2bcfd199116108c22021d5e58a91a78cfd18138493d7926f32870b84fce63ef8/68747470733a2f2f706f7365722e707567782e6f72672f63726561746976656f72616e67652f6c61726176656c2d696e6a65637461626c652f6c6963656e73652e737667)](https://packagist.org/packages/creativeorange/laravel-injectable)

A package for injecting variables into models after they were retrieved from the database.

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

[](#installation)

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](https://github.com/spatie/laravel-translatable) this package needs an extra plugin, read instructions in: [Laravel Injectable translatable extension](https://github.com/creativeorange/laravel-translatable-and-injectable)

Usage
-----

[](#usage)

In this example we make use of a `Question` model. This model has two attributes: `name` and `description`.

How are variables stored
------------------------

[](#how-are-variables-stored)

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`.

Setup the model
---------------

[](#setup-the-model)

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.

Per attribute
-------------

[](#per-attribute)

```
$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.

Per model
---------

[](#per-model)

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`.

Per request-cycle
-----------------

[](#per-request-cycle)

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
```

### Bonus: The Blade directive: Per request-cycle

[](#bonus-the-blade-directive-per-request-cycle)

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 --}}
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Credits
-------

[](#credits)

- [Jaco Tijssen](https://github.com/creativeorange)
- [Jonathan Hafkamp](https://github.com/creativeorange)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~463 days

Total

4

Last Release

628d ago

Major Versions

1.0.1 → v2.0.02022-05-04

v2.0.0 → v3.0.02024-08-19

PHP version history (2 changes)v1.0.0PHP &gt;=7.2.5

v2.0.0PHP &gt;=8.0.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/09c2af1aacfc3f405a56c5532985cb3b18f44520f24d9e2345d3a66acfc610ea?d=identicon)[creativeorange](/maintainers/creativeorange)

![](https://www.gravatar.com/avatar/84bfe6c95276cd48996191e1fe4989db99f86bbe5dfb2417eaa49e5283369783?d=identicon)[dejury](/maintainers/dejury)

![](https://www.gravatar.com/avatar/a146be61b865960b3aa78fa16fc31f1ce613921a77f7d5573b8c60a97598b377?d=identicon)[jacotijssen](/maintainers/jacotijssen)

---

Top Contributors

[![Edsardio](https://avatars.githubusercontent.com/u/10669035?v=4)](https://github.com/Edsardio "Edsardio (2 commits)")

---

Tags

creativeorangelaravel-injectable

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/creativeorange-laravel-injectable/health.svg)

```
[![Health](https://phpackages.com/badges/creativeorange-laravel-injectable/health.svg)](https://phpackages.com/packages/creativeorange-laravel-injectable)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M683](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M157](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M210](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M858](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
