PHPackages                             rozwell/composite-primary-keys - 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. rozwell/composite-primary-keys

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

rozwell/composite-primary-keys
==============================

Most advanced composite primary keys package with support of: binary columns, queueable, implicit route binding, relations

v1.12.1(5y ago)013MITPHPPHP &gt;=7.2

Since Jan 3Pushed 5y agoCompare

[ Source](https://github.com/rozwell/composite-primary-keys)[ Packagist](https://packagist.org/packages/rozwell/composite-primary-keys)[ RSS](/packages/rozwell-composite-primary-keys/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (14)Versions (36)Used By (0)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6c569e3b36327767fdc903a3224a5858c288ce4f5f80f3e9ce46de43ba056601/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d616b73696d72752f636f6d706f736974652d7072696d6172792d6b6579732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/maksimru/composite-primary-keys/?branch=master)[![codecov](https://camo.githubusercontent.com/495f2578332ccd9929faef0ede74434abc9e06a4a2fd0060e1727f18759186b3/68747470733a2f2f636f6465636f762e696f2f67682f6d616b73696d72752f636f6d706f736974652d7072696d6172792d6b6579732f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/maksimru/composite-primary-keys)[![StyleCI](https://camo.githubusercontent.com/11244e3bcabf8eb4b906da1352fa81304c70066cd13cf4445269afc53b5f9aeb/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3136333836343733372f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/163864737)[![CircleCI](https://camo.githubusercontent.com/0124b3a7a0146a753a4afa123968d0ebf17b8204ee8a0dec39b95b993bb39daa/68747470733a2f2f636972636c6563692e636f6d2f67682f6d616b73696d72752f636f6d706f736974652d7072696d6172792d6b6579732e7376673f7374796c653d737667)](https://circleci.com/gh/maksimru/composite-primary-keys)

About
-----

[](#about)

Library extends Laravel's Eloquent ORM with pretty full support of composite keys

Usage
-----

[](#usage)

Laravel 5.5

```
composer require maksimru/composite-primary-keys ~0.14
```

Laravel 5.6+

```
composer require maksimru/composite-primary-keys ~1.0
```

Simply add \\MaksimM\\CompositePrimaryKeys\\Http\\Traits\\HasCompositePrimaryKey trait into required models

Features Support
----------------

[](#features-support)

- increment and decrement
- update and save query
- binary columns

    ```
    class BinaryKeyUser extends Model
    {
        use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;

        protected $binaryColumns = [
            'user_id'
        ];

        protected $primaryKey = 'user_id';
    }
    ```

    With $hexBinaryColumns = false or omitted, $binaryKeyUser-&gt;user\_id will return binary value. BinaryKeyUser::find('BINARY\_VALUE') and BinaryKeyUser::create(\['id' =&gt; 'BINARY\_VALUE'\]) should be used in this case

    Optional ability to automatically encode binary values to their hex representation:

    ```
    class BinaryKeyUser extends Model
    {
      use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;

      protected $binaryColumns = [
          'user_id'
      ];

      protected $primaryKey = 'user_id';

      protected $hexBinaryColumns = true;
    }
    ```

    With $hexBinaryColumns = true, $binaryKeyUser-&gt;user\_id will return hex value. BinaryKeyUser::find('HEX\_VALUE') and BinaryKeyUser::create(\['id' =&gt; 'HEX\_VALUE'\]) should be used in this case

    *JSON output will contain hex values in both cases*
- model serialization in queues (with Illuminate\\Queue\\SerializesModels trait)

    Job:

    ```
    class TestJob implements ShouldQueue
    {
        use Queueable, SerializesModels;

        private $model;

        /**
         * Create a new job instance.
         */
        public function __construct(TestUser $testUser)
        {
            $this->model = $testUser;
        }

        /**
         * Execute the job.
         */
        public function handle()
        {
            $this->model->update(['counter' => 3333]);
        }
    }
    ```

    Dispatch:

    ```
    $model = TestUser::find([
        'user_id' => 1,
        'organization_id' => 100,
    ]);
    $this->dispatch(new TestJob($model));
    ```
- route implicit model binding support

    Model:

    ```
    class TestBinaryUser extends Model
    {
        use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;

        protected $table = 'binary_users';

        public $timestamps = false;

        protected $binaryColumns = [
          'user_id'
        ];

        protected $primaryKey = [
          'user_id',
          'organization_id',
        ];
    }
    ```

    routes.php:

    ```
    $router->get('binary-users/{binaryUser}', function (BinaryUser $binaryUser) {
        return $binaryUser->toJson();
    })->middleware('bindings')
    ```

    request:

    ```
    GET /binary-users/D9798CDF31C02D86B8B81CC119D94836___100
    ```

    response:

    ```
    {"user_id":"D9798CDF31C02D86B8B81CC119D94836","organization_id":"100","name":"Foo","user_id___organization_id":"D9798CDF31C02D86B8B81CC119D94836___100"}
    ```
- relations (only belongsTo relation is supported in this version)

    ```
    class TestUser extends Model
    {
        use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;

        protected $table = 'users';

        protected $primaryKey = [
            'user_id',
            'organization_id',
        ];

        public function referrer()
        {
            return $this->belongsTo(TestUser::class, [
                'referred_by_user_id',
                'referred_by_organization_id'
            ], [
                'user_id',
                'organization_id',
            ]);
        }
    }

    $referrer_user = $testUser->referrer()->first();
    ```

    will call

    ```
    select * from "users" where (("user_id" = ? and "organization_id" = ?)) limit 1
    ```

    with bindings \[ $testUser-&gt;referred\_by\_user\_id, $testUser-&gt;referred\_by\_organization\_id \]

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 87.1% 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 ~30 days

Recently: every ~93 days

Total

21

Last Release

2069d ago

Major Versions

v0.14 → v1.02019-08-26

PHP version history (3 changes)v0.10PHP &gt;=7

v1.0PHP &gt;=7.1

v1.12.1PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/cf6f0af82e84cd0d10e91bb5260b8393577fabbc7a1504c6a07d3f89d25b2b0e?d=identicon)[rozwell](/maintainers/rozwell)

---

Top Contributors

[![martianoff](https://avatars.githubusercontent.com/u/7222812?v=4)](https://github.com/martianoff "martianoff (61 commits)")[![rozwell](https://avatars.githubusercontent.com/u/695221?v=4)](https://github.com/rozwell "rozwell (4 commits)")[![alexkovt](https://avatars.githubusercontent.com/u/48892664?v=4)](https://github.com/alexkovt "alexkovt (3 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (2 commits)")

---

Tags

eloquentbinarycompositekeys

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rozwell-composite-primary-keys/health.svg)

```
[![Health](https://phpackages.com/badges/rozwell-composite-primary-keys/health.svg)](https://phpackages.com/packages/rozwell-composite-primary-keys)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[silber/bouncer

Eloquent roles and abilities.

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

Eloquent model validating trait.

9723.3M46](/packages/watson-validating)[dyrynda/laravel-model-uuid

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

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

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

591444.8k2](/packages/spiritix-lada-cache)

PHPackages © 2026

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