companue/shared-utilities

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

Maintainers

Package info

github.com/companue/shared-utilities

pkg:composer/companue/shared-utilities

Transparency log

Statistics

Installs: 6

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.2 2026-06-19 14:02 UTC

This package is auto-updated.

Last update: 2026-06-19 14:03:33 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

Request Timing Middleware

This package ships a shared request timing middleware that can be used across Laravel services.

  1. Add the middleware to your application's global HTTP middleware stack in app/Http/Kernel.php:
\Companue\SharedUtilities\Http\Middleware\LogRequestTiming::class,
  1. Use the environment key to enable or disable logging:
LOG_REQUEST_TIMING=true
  1. Optionally publish the package config if you want to customize it locally:
php artisan vendor:publish --tag=config

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