PHPackages                             mrgrain/eloquent-service-injection - 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. [Database &amp; ORM](/categories/database)
4. /
5. mrgrain/eloquent-service-injection

ActiveLibrary[Database &amp; ORM](/categories/database)

mrgrain/eloquent-service-injection
==================================

Simple trait to inject services into Eloquent models via a property.

1.1.0(9y ago)1921MITPHPPHP &gt;=5.5.9

Since Nov 8Pushed 7y ago1 watchersCompare

[ Source](https://github.com/mrgrain/eloquent-service-injection)[ Packagist](https://packagist.org/packages/mrgrain/eloquent-service-injection)[ RSS](/packages/mrgrain-eloquent-service-injection/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (4)Versions (3)Used By (0)

Eloquent Service Injection
==========================

[](#eloquent-service-injection)

Simple trait to inject services into Eloquent models via a property.

[![Build Status](https://camo.githubusercontent.com/a4b1374eaa5cf5d57bd6cbebf26812d360c45170931d22d3dc426d779c936721/68747470733a2f2f7472617669732d63692e6f72672f6d72677261696e2f656c6f7175656e742d736572766963652d696e6a656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mrgrain/eloquent-service-injection)[![Latest Stable Version](https://camo.githubusercontent.com/eebe90e38a32fdf24ed05ab5e28e559ad84eafd86f3714780c7a4118c29a466c/68747470733a2f2f706f7365722e707567782e6f72672f6d72677261696e2f656c6f7175656e742d736572766963652d696e6a656374696f6e2f762f737461626c65)](https://packagist.org/packages/mrgrain/eloquent-service-injection)[![Total Downloads](https://camo.githubusercontent.com/efdc01992e6115fcfdb27f9d83f71dc71bbd6fffc01db562530de155c6fbadf4/68747470733a2f2f706f7365722e707567782e6f72672f6d72677261696e2f656c6f7175656e742d736572766963652d696e6a656374696f6e2f646f776e6c6f616473)](https://packagist.org/packages/mrgrain/eloquent-service-injection)[![License](https://camo.githubusercontent.com/79fbc2acf9e056bc77c7463e03347b9cb066b97a5e11bea12501bc3f13a4df81/68747470733a2f2f706f7365722e707567782e6f72672f6d72677261696e2f656c6f7175656e742d736572766963652d696e6a656374696f6e2f6c6963656e7365)](https://packagist.org/packages/mrgrain/eloquent-service-injection)

Laravel's Eloquent models do not support constructor service injection. This trait aims to provide a simple and unified way of injection services into Eloquent models. It is doing that by using a property to define services, in a similar way how other model options can be defined (think `$casts` or `$with` for attribute casting and eager loading).

Requirements
============

[](#requirements)

- PHP &gt;= 5.6.0
- Laravel &gt;= 5.1 or Lumen &gt;= 5.1

Installation
============

[](#installation)

Add the package to your repository with Composer.

```
composer require mrgrain/eloquent-service-injection

```

Usage
=====

[](#usage)

Extend your models by using the trait:

```
namespace Mrgrain\EloquentServiceInjection;

class Comment extends Model {
    use ServiceInjectionTrait;
}
```

To always include the trait, add it to a base model all your models are extending from.

Add the services to be injected into the `$inject` property.

```
class Comment extends Model {
    use ServiceInjectionTrait;

    public $inject = [
        'storage' => Illuminate\Contracts\Filesystem\Filesystem::class
    ];
}
```

Use the array key, to access the service anywhere.

```
class Comment extends Model {
    use ServiceInjectionTrait;

    public $inject = [
        'storage' => Illuminate\Contracts\Filesystem\Filesystem::class
    ];

    public function storeAttachment($path, $contents)
    {
        $this->storage->put($path, $contents);
    }
}
```

Other usages
------------

[](#other-usages)

Wheres the trait has been designed for Eloquent models, it can be used in pretty much any class. Add and use it the same way as with models.

Contributing
============

[](#contributing)

First of all: Thank you for considering to contribute to this project. Without you the open source community would not be the same. If you decide to submit a pull request to this project, I'd kindly ask you to adhere to the following guidelines:

- Be aware of the scope of the project, to avoid disappointment.
- Please add appropriate tests to all pull requests.
- When interacting on this project, follow the [Code of Conduct](CODE_OF_CONDUCT.md).

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 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 ~462 days

Total

2

Last Release

3429d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/853d3ba7366c4236ac911df132e6830b872fa88d43c0e6fcff123d78066faa45?d=identicon)[mrgrain](/maintainers/mrgrain)

---

Top Contributors

[![mrgrain](https://avatars.githubusercontent.com/u/379814?v=4)](https://github.com/mrgrain "mrgrain (1 commits)")

---

Tags

eloquent-modelslaravelphpservice-injection

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mrgrain-eloquent-service-injection/health.svg)

```
[![Health](https://phpackages.com/badges/mrgrain-eloquent-service-injection/health.svg)](https://phpackages.com/packages/mrgrain-eloquent-service-injection)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k54.9M11.7k](/packages/illuminate-database)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[lemaur/eloquent-publishing

218.1k1](/packages/lemaur-eloquent-publishing)

PHPackages © 2026

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