notpurpell/composito

Composito is a small package that gives eloquent models the ability to have composite primary keys.

1.0.0 2020-03-07 11:00 UTC

This package is auto-updated.

Last update: 2025-07-07 23:50:08 UTC


README

Composito is a small package that gives eloquent models the ability to have composite primary keys through a trait.

Installation

You can install composito using Composer.

composer require notpurpell/composito

Usage

To add the composite primary key functionality to your eloquent model you must:

  1. Use the HasCompositePrimaryKey trait.
  2. Specify the composite primary key using the $primaryKey property.
  3. Set the $incrementing property to false.
use Composito\Traits\HasCompositePrimaryKey;

class MyModel extends Eloquent
{
    use HasCompositePrimaryKey;
    
    /**
     * Indicates if the IDs are auto-incrementing.
     * @var bool
     */
    public $incrementing = false;

    /**
     * The composite primary key of the model.
     * @var array
     */
    protected $primaryKey = [ "primary_one", "primary_two" ];

    ...
}

Then, you can use the create(), save(), update(), and delete() methods normally.

Support for other methods such as the find() method is coming soon.

Tests

The package contains some tests that can be run via phpunit.

./vendor/bin/phpunit

Credits

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.