PHPackages                             sebastiaanluca/laravel-route-model-autobinding - 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. sebastiaanluca/laravel-route-model-autobinding

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

sebastiaanluca/laravel-route-model-autobinding
==============================================

Automatically bind Eloquent models to be used as route segments.

5.0.0(5y ago)142.8k1MITPHPPHP ^7.3

Since Aug 6Pushed 5y ago1 watchersCompare

[ Source](https://github.com/sebastiaanluca/laravel-route-model-autobinding)[ Packagist](https://packagist.org/packages/sebastiaanluca/laravel-route-model-autobinding)[ Docs](https://github.com/sebastiaanluca/laravel-route-model-autobinding)[ RSS](/packages/sebastiaanluca-laravel-route-model-autobinding/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (4)Dependencies (5)Versions (13)Used By (1)

Automatically bind Eloquent models as route segment variables
=============================================================

[](#automatically-bind-eloquent-models-as-route-segment-variables)

[![Latest stable release](https://camo.githubusercontent.com/2279b8502bf56e9978f1297063a863577bf8f70b0663095df44de1975b3f64c3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7365626173746961616e6c7563612f6c61726176656c2d726f7574652d6d6f64656c2d6175746f62696e64696e672e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/sebastiaanluca/laravel-route-model-autobinding)[![Software license](https://camo.githubusercontent.com/08662bb339bee903c7279b82f3217310d3f196fdfa63b019d294ae85e1a5d7d7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d696e666f726d6174696f6e616c2e737667)](LICENSE.md)[![Build status](https://camo.githubusercontent.com/eab5cf97d555e6f68b953e42cec81c8db2b86f6398624f2e840ddf377b84eaa6/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7365626173746961616e6c7563612f6c61726176656c2d726f7574652d6d6f64656c2d6175746f62696e64696e672f6d61737465722e737667)](https://travis-ci.org/sebastiaanluca/laravel-route-model-autobinding)[![Total downloads](https://camo.githubusercontent.com/2e6cbac963b1fba836de758bc9f6b5d69a526cbee0dc90c96c04226539bcb1f4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7365626173746961616e6c7563612f6c61726176656c2d726f7574652d6d6f64656c2d6175746f62696e64696e672e7376673f636f6c6f723d627269676874677265656e)](https://packagist.org/packages/sebastiaanluca/laravel-route-model-autobinding)[![Total stars](https://camo.githubusercontent.com/6e9f0d6b287703eb4937bad9bf95300bcb8eb583e4089f628fdd862c6869118d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f7365626173746961616e6c7563612f6c61726176656c2d726f7574652d6d6f64656c2d6175746f62696e64696e672e7376673f636f6c6f723d627269676874677265656e)](https://github.com/sebastiaanluca/laravel-route-model-autobinding)

[![Read my blog](https://camo.githubusercontent.com/0760b582e22202bd412feb64f5f87b6459f64a0b8ea5fdd24cbadf87df736177/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d626c6f672d6c69676874677265792e737667)](https://blog.sebastiaanluca.com)[![View my other packages and projects](https://camo.githubusercontent.com/5ce3a83ee0ec2b4ae47a99f67347c1d8e78582757eca80fe31c98e5bd0e2185a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d6f746865725f7061636b616765732d6c69676874677265792e737667)](https://packagist.org/packages/sebastiaanluca)[![Follow @sebastiaanluca on Twitter](https://camo.githubusercontent.com/bde346ed8efc8b2458386b36256c64e3fe010276f449b80fe4f663c4c8bbd83e/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f7365626173746961616e6c7563612e7376673f7374796c653d736f6369616c)](https://twitter.com/sebastiaanluca)[![Share this package on Twitter](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?text=Automatically%20bind%20Eloquent%20models%20as%20route%20segment%20variables.%20Via%20@sebastiaanluca%20https://github.com/sebastiaanluca/laravel-route-model-autobinding)

**Immediately start using models in your routes without having to worry about maintaining any list or map.**

Medium to large Laravel applications can have *a lot* of models. If you're a heavy user of [route model binding](https://laravel.com/docs/5.6/routing#route-model-binding) to automatically retrieve and inject model instances in your controllers, that means you have to *manually* register dozens or hundreds of models in your route service provider. You then need to do the same for each new, changed, or deleted model which makes this nifty feature hard to maintain and easy to forget.

This package solves the issue of manually having to register each model by doing all the grunt work for you. It reads your `composer.json` PSR-4 autoload section and scans all their model directories for usable Eloquent models. It then explicitly binds each model into the router as a route segment variable using a case type of your choosing (see [configuring casing](#casing)).

### Example

[](#example)

Get rid of this boilerplate code:

```
public function boot() : void
{
    Route::model('user', \App\Users\User::class);
    Route::model('order', \App\Orders\Order::class);
    Route::model('shoppingCart', \App\Carts\Cart::class);
    Route::model('Item', \App\Item::class);
    Route::model('Admin', \App\Users\Admin::class);
    … (repeat dozens of times)
}
```

And just do this (for any Eloquent model in your application):

```
Route::get('profile/{user}', ShowProfile::class);
Route::get('orders/{order}', ShowOrder::class);
Route::get('carts/{shoppingCart}', ShowShoppingCart::class);
…
```

Table of contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [How to install](#how-to-install)
- [How to use](#how-to-use)
    - [Defining model namespaces](#defining-model-namespaces)
    - [Route segment variables](#route-segment-variables)
    - [Caching bindings for production](#caching-bindings-for-production)
    - [Configuration](#configuration)
        - [Casing](#casing)
- [License](#license)
- [Change log](#change-log)
- [Testing](#testing)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)
- [About](#about)

Requirements
------------

[](#requirements)

- PHP 7.3 or higher
- Laravel 7.0 or higher

How to install
--------------

[](#how-to-install)

Via Composer:

```
composer require sebastiaanluca/laravel-route-model-autobinding
```

How to use
----------

[](#how-to-use)

### Defining model namespaces

[](#defining-model-namespaces)

Laravel route model autobinding uses your `composer.json` PSR-4 autoload section to know which namespaces and paths to scan. In any new Laravel project, the default `App\\` namespace is already in place, so for most projects no additional setup required. If you have other namespaces registered like local modules or (dev) packages, those will be scanned too.

```
{
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "MyModule\\": "modules/MyModule/",
            "MyPackage\\": "MyPackage/src/"
        }
    }
}
```

Furthermore it filters out traits, abstract classes, helper files, and other unusable items to only bind valid Eloquent models.

### Route segment variables

[](#route-segment-variables)

After installing the package, you can immediately get to work using the aliased Eloquent models in your routes:

```
Route::get('profile/{user}', ShowProfile::class);
Route::get('orders/{order}', ShowOrder::class);
Route::get('carts/{shoppingCart}', ShowShoppingCart::class);
…
```

Besides scanning and aliasing your models for you, this package alters no native Laravel functionality. Therefore, see the Laravel documentation on how to use [route model injection](https://laravel.com/docs/5.6/routing#route-model-binding).

### Caching bindings in production

[](#caching-bindings-in-production)

To cache all bindings and speed up your application in production, add the cache command to your deploy scripts:

```
php artisan autobinding:cache

```

This scans all your current models and writes a static cache file to the `bootstrap/cache` directory. Upon subsequent framework booting, it reads the cache file instead of scanning and aliasing on-the-fly.

Note that this thus **disables runtime scanning**, meaning new models will not be recognized and changes to existing models will not be reflected (not very handy during development). You can however still change the case type in the configuration file, as the binding happens in a later stage.

To clear the cache file, run:

```
php artisan autobinding:clear

```

### Configuration

[](#configuration)

Run

```
php artisan vendor:publish

```

and select

```
laravel-route-model-autobinding (configuration)

```

to publish the configuration file.

#### Casing

[](#casing)

By default, the case type for aliasing models is set to *camel case*. You can change this to use camel, snake, or studly casing.

See `\SebastiaanLuca\RouteModelAutobinding\CaseTypes` for possible options.

Camel case (default):

```
Route::get('carts/{shoppingCart}', ShowShoppingCart::class);
```

Snake case:

```
Route::get('carts/{shopping_cart}', ShowShoppingCart::class);
```

Studly case:

```
Route::get('carts/{ShoppingCart}', ShowShoppingCart::class);
```

The case type can still be changed after caching your models.

License
-------

[](#license)

This package operates under the MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.

Change log
----------

[](#change-log)

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

Testing
-------

[](#testing)

```
composer install
composer test
```

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE OF CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Sebastiaan Luca](https://github.com/sebastiaanluca)
- [All Contributors](../../contributors)

About
-----

[](#about)

My name is Sebastiaan and I'm a freelance back-end developer specializing in building custom Laravel applications. Check out my [portfolio](https://www.sebastiaanluca.com) for more information, [my blog](https://blog.sebastiaanluca.com) for the latest tips and tricks, and my other [packages](https://packagist.org/packages/sebastiaanluca) to kick-start your next project.

Have a project that could use some guidance? Send me an e-mail at !

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

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

Recently: every ~118 days

Total

11

Last Release

2037d ago

Major Versions

1.1.0 → 2.0.02019-03-01

2.0.3 → 3.0.02019-09-06

3.0.1 → 4.0.02020-04-24

4.0.0 → 5.0.02020-10-19

PHP version history (2 changes)1.0.0PHP ^7.2

4.0.0PHP ^7.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/711940?v=4)[Sebastiaan Luca](/maintainers/sebastiaanluca)[@sebastiaanluca](https://github.com/sebastiaanluca)

---

Top Contributors

[![sebastiaanluca](https://avatars.githubusercontent.com/u/711940?v=4)](https://github.com/sebastiaanluca "sebastiaanluca (61 commits)")

---

Tags

bindeloquentlaravelmodelphproutephplaravelmodeleloquentroutebind

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sebastiaanluca-laravel-route-model-autobinding/health.svg)

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

###  Alternatives

[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[sebastiaanluca/laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).

40111.7k1](/packages/sebastiaanluca-laravel-boolean-dates)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[kiwilan/typescriptable-laravel

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. If you want to use some helpers with Inertia, you can install associated NPM package.

3920.9k](/packages/kiwilan-typescriptable-laravel)[waad/laravel-model-metadata

A robust Laravel package for handling metadata with JSON casting, custom relation names, and advanced querying capabilities.

823.1k](/packages/waad-laravel-model-metadata)[rayvenues/eloquent-model-generator

Eloquent Model Generator

1325.2k1](/packages/rayvenues-eloquent-model-generator)

PHPackages © 2026

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