companue/shared-utilities

Shared utilities, traits, and helpers for Companue service packages including ordering, timestamping, and common model behaviors

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/companue/shared-utilities

1.0.0 2025-12-27 17:46 UTC

This package is auto-updated.

Last update: 2025-12-27 17:49:50 UTC


README

Shared utilities, traits, and helpers for Companue service packages.

Features

  • Orderable Trait: Easy ordering/ranking capabilities for Eloquent models
    • Handles unique constraint conflicts with two-pass approach
    • Built-in scopes for querying ordered data
    • Bulk and single-item reordering support

Installation

composer require companue/shared-utilities

Usage

Orderable Trait

Use the trait in your model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Companue\SharedUtilities\Traits\Orderable;

class Joblevel extends Model
{
    use Orderable;

    public function orderingAttr(): string
    {
        return 'priority'; // Your ordering column
    }
}

Available Methods

  • ordered() - Scope to order query results
  • orderedDesc() - Scope to reverse order
  • getOrderingValue() - Get the ordering value
  • setOrderingValue($value) - Set the ordering value
  • reorderBatch(array $items) - Bulk reorder with unique constraint handling
  • reorderSingle($id, $position) - Move single item and shift others
  • getNextOrderingValue() - Get next available position

Example

// Get ordered items
$items = Joblevel::ordered()->get();

// Reorder multiple items
Joblevel::reorderBatch([
    ['id' => 1, 'priority' => 1],
    ['id' => 2, 'priority' => 2],
    ['id' => 3, 'priority' => 3],
]);

// Move single item
Joblevel::reorderSingle(5, 2); // Move item 5 to position 2

License

MIT