PHPackages                             kayrunm/polybind - 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. [API Development](/categories/api)
4. /
5. kayrunm/polybind

ActiveLibrary[API Development](/categories/api)

kayrunm/polybind
================

Polymorphic route-model binding for Laravel.

0.2(2y ago)54MITPHPPHP ^8.2

Since Sep 14Pushed 2y ago1 watchersCompare

[ Source](https://github.com/kayrunm/polybind)[ Packagist](https://packagist.org/packages/kayrunm/polybind)[ RSS](/packages/kayrunm-polybind/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

Polybind
========

[](#polybind)

Polymorphic route-model binding for Laravel.

Pre-requisites
--------------

[](#pre-requisites)

This package requires the following:

- PHP 8.2
- Laravel 10 or higher

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

[](#installation)

To install the package, you simply need to run the following command:

```
composer require kayrunm/polybind
```

The service provider for the package will automatically be registered, but if you wish, you can optionally add the following in your `config/app.php` file:

```
'providers' => [
    // ...
    Kayrunm\Polybind\PolybindServiceProvider::class,
],
```

Finally, you just need to alias the middleware in your `$middlewareAliases` in `app/Http/Kernel.php`:

```
protected $middlewareAliases = [
    // ...
    'polybind' => Kayrunm\Polybind\Polybind::class
];
```

You can skip aliasing the middleware if you have no intention of using [per-route parameters](#per-route-configuration), as it just makes it easier to add the middleware parameters. If you do skip this step, you'll have to apply the middleware using the full Polybind classname.

Configuration
-------------

[](#configuration)

This package allows for you to configure the default route parameters and the resolution logic. To start with this, you need to publish the configuration file:

```
php artisan vendor:publish --provider="Kayrunm\Polybind\PolybindServiceProvider"
```

You can now edit the `config/polybind.php` file with your chosen defaults.

Usage
-----

[](#usage)

Polybind works via a middleware that you add to your polymorphic routes. The simplest way to get started is to add the middleware to your route definition, like so:

```
// routes/web.php

Route::get('/{model_type}/{model_id}', [MyController::class, 'show'])->middleware('polybind');
```

Polybind will then do its magic when you access this route, by automatically resolving the model and allowing you to access the model in your controller method via the `$model` parameter:

```
// MyController.php

public function show($model)
{
    return response()->json($model);
}
```

**Note:** Polybind requires that your models are registered in `Relation::morphMap()`.

### Type validation

[](#type-validation)

Polybind allows you to hint the types of model that a route accepts, either with union/intersection types or even with interfaces. If Polybind resolves a model that doesn't match the type that you have type hinted, it will throw a `Kayrum\Polybind\Exceptions\InvalidModelType` exception. If you don't use any type hinting in your controller method, Polybind will allow any Model to be resolved.

Here's an example of type hinting with an interface:

```
// MyController.php

public function show(HasAuthor $model)
{
    return response()->json($model);
}
```

And here's an example of type hinting using a union type:

```
// MyController.php

public function show(Post|Comment $model)
{
    return response()->json($model);
}
```

### Per-route configuration

[](#per-route-configuration)

Polybind also allows you to configure the route parameters for the model type and model identifier, as well as the name you use for the parameter in your controller method, on a per-route basis. Here's an example of how to do that:

```
// routes/web.php

Route::get('/{author_type}/{author_uuid}', function ($author) {
    return response()->json($author);
})->middleware('polybind:author_type,author_uuid,author');
```

### Adding to your entire application

[](#adding-to-your-entire-application)

If you make use of polymorphic route-model binding throughout your application, you may find it easier to simply apply Polybind's functionality on all of your routes. Polybind will only run on routes where it finds a matching type *and*identifier parameter.

To do this, simply add the Polybind middleware to the middleware groups you would like it to run on, for example in the `web` and `api` groups in `app/Http/Kernel.php`:

```
protected $middlewareGroups = [
    'web' => [
        // ...
        \Kayrunm\Polybind\Polybind::class,
    ],

    'api' => [
        // ...
        \Kayrunm\Polybind\Polybind::class,
    ],
];
```

**Note:** Make sure that the Polybind middleware is applied *after* the `SubstituteBindings` middleware.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

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

Total

2

Last Release

976d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6943d16d8939d4ce7d5f4d4df1fbb6b12d5d2834bacb07dd2efb82b7ca278377?d=identicon)[kayrunm](/maintainers/kayrunm)

---

Top Contributors

[![kayrunm](https://avatars.githubusercontent.com/u/4512646?v=4)](https://github.com/kayrunm "kayrunm (19 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kayrunm-polybind/health.svg)

```
[![Health](https://phpackages.com/badges/kayrunm-polybind/health.svg)](https://phpackages.com/packages/kayrunm-polybind)
```

###  Alternatives

[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k26.9M220](/packages/spatie-laravel-query-builder)[flat3/lodata

OData v4.01 Producer for Laravel

96320.9k](/packages/flat3-lodata)[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[ryangjchandler/bearer

Minimalistic token-based authentication for Laravel API endpoints.

8129.8k](/packages/ryangjchandler-bearer)[api-platform/laravel

API Platform support for Laravel

59126.4k6](/packages/api-platform-laravel)[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

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