New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

soyhuce/laravel-model-injection

Extended Model injection for Laravel
14,848
Install
composer require soyhuce/laravel-model-injection
Latest Version:2.5.0
PHP:^8.3
Laravel:11 · 12
Older releases:9 · 10
License:MIT
Last Updated:Apr 20, 2026
Links: GitHub  ·  Packagist
Maintainer: SoyHuCe

Extended Model injection for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

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

You can install the package via composer:

composer require soyhuce/laravel-model-injection

Usage

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<string, mixed>
     */
    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 https://laravel.com/docs/routing#implicit-binding

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

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

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

soyhuce/phpstan-extension

Extra rules for phpstan analysis

23,747 1
soyhuce/dev-tools

Laravel dev-tools

17,145 1
soyhuce/laravel-testing

Helpers for Laravel tests

15,875 3
soyhuce/laravel-json-resources

An opinionated JsonResource for Laravel

15,204 2
soyhuce/laravel-fluent-policy

Write fluent policies in Laravel

14,214 2

Version History

Version Released PHP Laravel License
2.5.0 ^8.3 ^11.0|^12.0 MIT
2.4.0 ^8.3 ^11.0|^12.0 MIT
2.3.0 ^8.3 ^11.0 MIT
2.2.0 ^8.2 ^10.0 || ^11.0 MIT
2.1.0 ^8.2 ^10.0 MIT