PHPackages                             soyhuce/laravel-model-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. soyhuce/laravel-model-injection

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

soyhuce/laravel-model-injection
===============================

Extended Model injection for Laravel

2.5.0(2mo ago)013.2k1[5 PRs](https://github.com/Soyhuce/laravel-model-injection/pulls)MITPHPPHP ^8.3CI passing

Since Mar 3Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/Soyhuce/laravel-model-injection)[ Packagist](https://packagist.org/packages/soyhuce/laravel-model-injection)[ Docs](https://github.com/soyhuce/laravel-model-injection)[ GitHub Sponsors](https://github.com/soyhuce)[ RSS](/packages/soyhuce-laravel-model-injection/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (11)Versions (14)Used By (0)

Extended Model injection for Laravel
====================================

[](#extended-model-injection-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3f84330ad354b479bc374cff64abe76f446bb5dff4b0a6b3de7799950d9bc631/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f79687563652f6c61726176656c2d6d6f64656c2d696e6a656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/soyhuce/laravel-model-injection)[![GitHub Tests Action Status](https://camo.githubusercontent.com/d23a4ba04cf789fc12c598cd5e1520347e4b5d92d2dcc6d717663525795180b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f79687563652f6c61726176656c2d6d6f64656c2d696e6a656374696f6e2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/soyhuce/laravel-model-injection/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/627ba2c583873a7fb6eb72f8bc0c18052fe0bd24e4af456d5498724cb6c92ca6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f79687563652f6c61726176656c2d6d6f64656c2d696e6a656374696f6e2f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/soyhuce/laravel-model-injection/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![GitHub PHPStan Action Status](https://camo.githubusercontent.com/4a3613665c79fe566b08e9545f84acb081cddeb1649e50500ca4aedae00c41f9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f79687563652f6c61726176656c2d6d6f64656c2d696e6a656374696f6e2f7068707374616e2e796d6c3f6272616e63683d6d61696e266c6162656c3d7068707374616e)](https://github.com/soyhuce/laravel-model-injection/actions?query=workflow%3APHPStan+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/4543939a8659c05b467823861dd8caf570be4a1e2f7235c880517aae6edd0096/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f79687563652f6c61726176656c2d6d6f64656c2d696e6a656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/soyhuce/laravel-model-injection)

Want to control have better control of model injection ? Need to validate the data before querying the database ?

Here is a package that allows you to do that.

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

[](#installation)

You can install the package via composer:

```
composer require soyhuce/laravel-model-injection
```

Usage
-----

[](#usage)

### Implicit binding

[](#implicit-binding)

To validate the url parameter used to inject the model in the controller, you can use the `Soyhuce\ModelInjection\ValidatesImplicitBindings` trait in it.

You will have then to implement the method `public function routeBindingRules(): array`which will define, for each key on which the model will be bound, the rules to validate the url parameter.

```
use Soyhuce\ModelInjection\ValidatesImplicitBinding;

class Post extends Model
{
    use ValidatesImplicitBinding;

    /**
     * @return array
     */
    public function routeBindingRules(): array
    {
        return [
            'id' => 'integer',
            'slug' => ['string', 'min:5']
        ];
    }
}
```

This will allow you to validate the parameter to bind the `Post` in the routes using:

```
Route::get('posts/{post}', function(Post $post) {
    //...
});

Route::get('posts-by-slug/{post:slug}', function(Post $post) {
    //...
});
```

If the parameter is not valid, a 404 error will be returned.

```
GET /posts/foo => 404
GET /posts-by-slug/bar => 404

```

See

#### Customize implicit route binding error

[](#customize-implicit-route-binding-error)

You can customize the way this package will handle validation failure for implicit bindings.

In a Service provider, just call `InvalidRouteBinding::handleUsing` :

```
InvalidRouteBinding::handleUsing(function (string $class, string $field): never {
    Log::error("Invalid binding for $class on $field.");

    abort(422);
});
```

### Explicit bindings

[](#explicit-bindings)

You can explicitly bind your models using `\Soyhuce\ModelInjection\BindModels` trait in a service provider (`RouteServiceProvider` for exemple).

```
use Soyhuce\ModelInjection\BindModels;

class RouteServiceProvider extends ServiceProvider {

    use BindModels;

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot() {
        parent::boot();

        $this->bindModel('user', User::class, 'integer'); // Validates that the parameter is an integer

        // You can bind a model explicitly on a given column
        $this->bindModelOn('post', Post::class, ['string', 'min:5'], 'slug');
    }
}
```

If the given parameter is not valid, a 404 error will be returned.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Bastien Philippe](https://github.com/bastien-phi)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance89

Actively maintained with recent releases

Popularity25

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~210 days

Total

9

Last Release

74d ago

Major Versions

1.2.0 → 2.0.02023-04-28

PHP version history (4 changes)1.0.0PHP ^8.0

1.1.0PHP ^8.1

2.1.0PHP ^8.2

2.3.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/206cfbf866a463f7e7d1e86946d59b82f4191b9c89f9981fb03eeb264d77af79?d=identicon)[SoyHuCe](/maintainers/SoyHuCe)

---

Top Contributors

[![bastien-phi](https://avatars.githubusercontent.com/u/10199039?v=4)](https://github.com/bastien-phi "bastien-phi (30 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (18 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (12 commits)")[![EdenMl](https://avatars.githubusercontent.com/u/70885551?v=4)](https://github.com/EdenMl "EdenMl (3 commits)")[![tnajah59](https://avatars.githubusercontent.com/u/91531929?v=4)](https://github.com/tnajah59 "tnajah59 (1 commits)")

---

Tags

hacktoberfestinjectionlaravellaravelsoyhucelaravel-model-injection

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/soyhuce-laravel-model-injection/health.svg)

```
[![Health](https://phpackages.com/badges/soyhuce-laravel-model-injection/health.svg)](https://phpackages.com/packages/soyhuce-laravel-model-injection)
```

###  Alternatives

[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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