- How do I install lexorank-php in a Laravel project?
- Use Composer: `composer require alexcrawford/lexorank-php`. Register the service provider in `config/app.php` under `providers`, then bind it in `AppServiceProvider` with `app()->singleton(Lexorank::class, fn() => new Lexorank());`. For quick use, autoload the class directly.
- Does lexorank-php work with Laravel Eloquent models?
- Yes. Store lexorank strings as VARCHAR in your database, then create custom Eloquent scopes like `scopeOrderedByRank()` to sort queries in-memory. For large datasets, cache sorted results in Redis to avoid repeated string comparisons.
- What Laravel versions does this package support?
- lexorank-php is PHP 8.0+ compatible and works with Laravel 8.x, 9.x, and 10.x. It’s framework-agnostic but designed for Laravel’s dependency injection. Tested with PHPUnit for edge cases like concurrent writes.
- Can I use lexorank for a real-time kanban board with drag-and-drop?
- Absolutely. LexoRank’s O(1) insertion makes it ideal for drag-and-drop UIs. Store ranks in your database, update them atomically via transactions, and sort client-side or in-memory. For high concurrency, use Redis for lock-free rank generation.
- How does lexorank handle collisions or duplicate ranks?
- LexoRank is mathematically designed to avoid collisions. Each rank string is unique and collision-resistant, even with millions of items. No hashing or randomness is involved—just deterministic string generation.
- Will lexorank strings bloat my database over time?
- String length grows logarithmically with list size (e.g., ~20 chars for 10k items). For very large lists, monitor storage and consider hybrid approaches: use lexorank for dynamic sections and auto-increment IDs for static ones.
- How do I migrate an existing ordered list (e.g., ORDER BY id) to lexorank?
- Start with a shadow mode: dual-write lexorank strings alongside your current IDs. Gradually replace queries with lexorank-based sorting. For Eloquent, add a `lexorank` column and backfill it using `Lexorank::insertAfter($lastRank)` for each item.
- Is lexorank-php faster than PostgreSQL’s list type or MongoDB’s $sort?
- LexoRank excels in **insertion speed** (O(1) vs. O(n) for numeric IDs). For **query speed**, PostgreSQL’s list type or MongoDB’s native sorting may outperform PHP-side string comparisons. Benchmark your use case—lexorank shines in dynamic, high-write scenarios.
- How do I handle concurrent rank updates in a distributed Laravel app?
- Use database transactions or Redis locks to prevent race conditions. For example, wrap rank updates in `DB::transaction()` or use `Redis::lock()`. Avoid optimistic locking unless your system has low contention.
- Are there alternatives to lexorank-php for Laravel?
- For Laravel, alternatives include: **PostgreSQL’s `list` type** (native sorting), **MongoDB’s `$sort` with custom logic**, or **numeric IDs with `position` columns** (but O(n) updates). LexoRank stands out for portability and O(1) inserts across databases.