jeidison/composite-key

Laravel composite key

0.4.0 2021-09-15 15:45 UTC

This package is auto-updated.

Last update: 2024-12-15 22:56:54 UTC


README

==========

The problem

Eloquent doesn't support composite keys.

Installation

$ composer require jeidison/composite-key

Usage

Add Trait of your model

namespace App;

use Illuminate\Database\Eloquent\Model;

class MyClass extends Model
{
    use Jeidison\CompositeKey\CompositeKey;
    
    ...
}
  • Find
    public function index()
    {
        $modelX = ModelX::find(1);
        // or 
        $modelX = ModelX::find(['c1' => 1, 'c2' => 2]);
    }
  • FindOrFail
    public function index()
    {
        $modelX = ModelX::findOrFail(1);
        // or 
        $modelX = ModelX::findOrFail(['c1' => 1, 'c2' => 2]);
    }
  • FindMany
    public function index()
    {
        $modelX = ModelX::findMany([['c1' => 1, 'c2' => 2]]);
        // or 
        $modelX = ModelX::findMany([['c1' => 1, 'c2' => 2], ['a1' => 1, 'a2' => 2]]);
    }
  • Model Fresh
    public function index()
    {
        $modelX      = ModelX::find(1);
        $freshModelX = $modelX->fresh();
    }
  • Model Refresh
    public function index()
    {
        $anyWhatever      = Anything::find(1);
        $freshAnyWhatever = $anyWhatever->refresh();
    }
  • Model Delete
    public function index()
    {
        $modelX = ModelX::find(1);
        $modelX->delete();
    }
  • Model Destroy
    public function index()
    {
        $count = ModelX::destroy(['c1' => 1, 'c2' => 2]);
        // or 
        $count = ModelX::destroy([['c1' => 1, 'c2' => 2], ['a1' => 1, 'a2' => 2]]);
    }
  • FirstOrCreate
    $modelX = ModelX::firstOrCreate(['name' => 'Test 10']);
    
    $modelX = ModelX::firstOrCreate(
        ['name' => 'Test 10'],
        ['delayed' => 1, 'arrival_time' => '11:30']
    );
  • FirstOrNew
    $modelX = ModelX::firstOrNew(['name' => 'Test 10']);
    
    $modelX = ModelX::firstOrNew(
        ['name' => 'Test 10'],
        ['delayed' => 1, 'arrival_time' => '11:30']
    );
  • IncrementOrDecrement
    // Not implemented
  • Eloquent\Collection::find
    // Not implemented
  • Eloquent\Collection::fresh
    // Not implemented
  • Authenticatable::getAuthIdentifierName
    // Not implemented
  • Authenticatable::getAuthIdentifier
    // Not implemented
  • Model::getRouteKey
    // Not implemented
  • Model::getRouteKeyName
    // Not implemented
  • SerializesAndRestoresModelIdentifiers
    // Not implemented

Authors

License

composite-key is licensed under the MIT License.