rozwell / composite-primary-keys
Most advanced composite primary keys package with support of: binary columns, queueable, implicit route binding, relations
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 12
pkg:composer/rozwell/composite-primary-keys
Requires
- php: >=7.2
 - ext-json: *
 - illuminate/cache: ^6.0 | ^7.0
 - illuminate/contracts: ^6.0 | ^7.0
 - illuminate/queue: ^6.0 | ^7.0
 - illuminate/redis: ^6.0 | ^7.0
 - illuminate/session: ^6.0 | ^7.0
 - illuminate/support: ^6.0 | ^7.0
 
Requires (Dev)
- barryvdh/laravel-debugbar: ^3.1 | ^3.3
 - fzaninotto/faker: ~1.4 | ^1.9
 - laravel/framework: ^6.0 | ^7.0
 - mockery/mockery: 0.9.* | ^1.4
 - orchestra/database: ~4.0 | ^5.1
 - orchestra/testbench: ~4.0 | ^5.2
 - orchestra/testbench-browser-kit: ~4.0 | ^5.2
 - phpunit/phpunit: ^7 | ^8
 
- dev-master
 - v1.12.1
 - v1.08.2
 - v1.08.1
 - v1.08
 - v1.07
 - v1.05
 - v1.04.5
 - v1.04.4
 - v1.04.3
 - v1.04.2
 - v1.04.1
 - v1.04
 - v1.03
 - v1.02
 - v1.01
 - v1.0
 - v0.14
 - v0.13
 - v0.12
 - v0.11
 - v0.10
 - dev-fix/missing-relations
 - dev-analysis-zGBPxo
 - dev-fix/binary-model-restoration
 - dev-analysis-z3LbmP
 - dev-analysis-q19PK3
 - dev-analysis-qxVZNa
 - dev-fix/optional-hex-keys
 - dev-analysis-XkwGp9
 - dev-analysis-8mANnb
 - dev-feature/auto-binary-keys
 - dev-analysis-qyaQ5G
 - dev-laravel-5.6
 - dev-analysis-zY4bEv
 - dev-analysis-qgl6Kw
 
This package is auto-updated.
Last update: 2025-10-09 08:37:18 UTC
README
About
Library extends Laravel's Eloquent ORM with pretty full support of composite keys
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
- 
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->user_id will return binary value. BinaryKeyUser::find('BINARY_VALUE') and BinaryKeyUser::create(['id' => '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->user_id will return hex value. BinaryKeyUser::find('HEX_VALUE') and BinaryKeyUser::create(['id' => '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->referred_by_user_id, $testUser->referred_by_organization_id ]