PHP code example of maksimru / composite-primary-keys

1. Go to this page and download the library: Download maksimru/composite-primary-keys library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maksimru / composite-primary-keys example snippets

  
    class BinaryKeyUser extends Model
    {
        use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;
    
        protected $binaryColumns = [
            'user_id'
        ];
    
        protected $primaryKey = 'user_id';
    }
    
  
    class BinaryKeyUser extends Model
    {
      use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;
    
      protected $binaryColumns = [
          'user_id'
      ];
    
      protected $primaryKey = 'user_id';
    
      protected $hexBinaryColumns = true;
    }
    

    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]);
        }
    }
    

    $model = TestUser::find([
        'user_id' => 1,
        'organization_id' => 100,
    ]);
    $this->dispatch(new TestJob($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',
        ];
    }
    

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

  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();