{"id":21992410,"url":"https://github.com/soyhuce/laravel-model-injection","last_synced_at":"2025-04-14T10:13:59.759Z","repository":{"id":41877070,"uuid":"465673125","full_name":"Soyhuce/laravel-model-injection","owner":"Soyhuce","description":"Extended Model injection for Laravel","archived":false,"fork":false,"pushed_at":"2025-02-24T08:53:56.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-14T10:13:52.791Z","etag":null,"topics":["hacktoberfest","injection","laravel"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Soyhuce.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"soyhuce"}},"created_at":"2022-03-03T10:32:35.000Z","updated_at":"2025-02-24T08:53:59.000Z","dependencies_parsed_at":"2023-11-07T11:29:08.835Z","dependency_job_id":"c8d3a48c-9f61-4653-895d-aeba81e9c3fd","html_url":"https://github.com/Soyhuce/laravel-model-injection","commit_stats":{"total_commits":20,"total_committers":5,"mean_commits":4.0,"dds":0.65,"last_synced_commit":"ab15a4b14a6339253448b90ba8018c3ed3a2df7f"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":"Soyhuce/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Flaravel-model-injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Flaravel-model-injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Flaravel-model-injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Flaravel-model-injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Soyhuce","download_url":"https://codeload.github.com/Soyhuce/laravel-model-injection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248860167,"owners_count":21173342,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","injection","laravel"],"created_at":"2024-11-29T20:13:42.851Z","updated_at":"2025-04-14T10:13:59.710Z","avatar_url":"https://github.com/Soyhuce.png","language":"PHP","funding_links":["https://github.com/sponsors/soyhuce"],"categories":[],"sub_categories":[],"readme":"# Extended Model injection for Laravel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/soyhuce/laravel-model-injection.svg?style=flat-square)](https://packagist.org/packages/soyhuce/laravel-model-injection)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/laravel-model-injection/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/soyhuce/laravel-model-injection/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/laravel-model-injection/php-cs-fixer.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/soyhuce/laravel-model-injection/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![GitHub PHPStan Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/laravel-model-injection/phpstan.yml?branch=main\u0026label=phpstan)](https://github.com/soyhuce/laravel-model-injection/actions?query=workflow%3APHPStan+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/soyhuce/laravel-model-injection.svg?style=flat-square)](https://packagist.org/packages/soyhuce/laravel-model-injection)\n\nWant to control have better control of model injection ? Need to validate the data before querying the database ?\n\nHere is a package that allows you to do that.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require soyhuce/laravel-model-injection\n```\n\n## Usage\n\n### Implicit binding\n\nTo validate the url parameter used to inject the model in the controller, you can use\nthe `Soyhuce\\ModelInjection\\ValidatesImplicitBindings` trait in it.\n\nYou will have then to implement the method `public function routeBindingRules(): array`\nwhich will define, for each key on which the model will be bound, the rules to validate the url parameter.\n\n```php\nuse Soyhuce\\ModelInjection\\ValidatesImplicitBinding;\n\nclass Post extends Model \n{\n    use ValidatesImplicitBinding;\n    \n    /**\n     * @return array\u003cstring, mixed\u003e\n     */\n    public function routeBindingRules(): array\n    {\n        return [\n            'id' =\u003e 'integer',\n            'slug' =\u003e ['string', 'min:5']\n        ];\n    }\n}\n```\n\nThis will allow you to validate the parameter to bind the `Post` in the routes using:\n\n```php\nRoute::get('posts/{post}', function(Post $post) {\n    //...\n});\n\nRoute::get('posts-by-slug/{post:slug}', function(Post $post) {\n    //...\n});\n```\n\nIf the parameter is not valid, a 404 error will be returned.\n\n```\nGET /posts/foo =\u003e 404\nGET /posts-by-slug/bar =\u003e 404\n```\n\nSee [https://laravel.com/docs/routing#implicit-binding](https://laravel.com/docs/routing#implicit-binding)\n\n#### Customize implicit route binding error\n\nYou can customize the way this package will handle validation failure for implicit bindings.\n\nIn a Service provider, just call `InvalidRouteBinding::handleUsing` :\n\n```php\nInvalidRouteBinding::handleUsing(function (string $class, string $field): never {\n    Log::error(\"Invalid binding for $class on $field.\");\n\n    abort(422);\n});\n```\n\n### Explicit bindings\n\nYou can explicitly bind your models using `\\Soyhuce\\ModelInjection\\BindModels` trait in a service\nprovider (`RouteServiceProvider` for exemple).\n\n```php\nuse Soyhuce\\ModelInjection\\BindModels;\n\nclass RouteServiceProvider extends ServiceProvider {\n\n    use BindModels;\n\n    /**\n     * Define your route model bindings, pattern filters, etc.\n     *\n     * @return void\n     */\n    public function boot() {\n        parent::boot();\n\n        $this-\u003ebindModel('user', User::class, 'integer'); // Validates that the parameter is an integer\n        \n        // You can bind a model explicitly on a given column\n        $this-\u003ebindModelOn('post', Post::class, ['string', 'min:5'], 'slug');\n    }\n}\n\n```\n\nIf the given parameter is not valid, a 404 error will be returned.\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Bastien Philippe](https://github.com/bastien-phi)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyhuce%2Flaravel-model-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoyhuce%2Flaravel-model-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyhuce%2Flaravel-model-injection/lists"}