Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Comparison Laravel Package

php-standard-library/comparison

Small PHP comparison utilities that provide consistent, reusable ways to compare values. Useful for sorting, equality checks, and custom comparator functions without rewriting boilerplate across your projects.

View on GitHub
Deep Wiki
Context7

Interfaces and functions for type-safe, consistent value comparison

Frequently asked questions about Comparison
How do I integrate this package into a Laravel project for sorting Eloquent collections?
Use the package’s `Comparator` class with Laravel Collections. Replace `sortBy()` with `Comparator::asc('price')` or `Comparator::desc('created_at')` for consistent, reusable sorting. For Eloquent queries, create a query macro to bridge the gap between the comparator and `orderBy()`. Example: `DB::macro('orderByComparator', fn($query, $field, $direction) => $query->orderBy($field, $direction === 'asc' ? 'asc' : 'desc'));`
Does this package support Laravel’s Query Builder for database-level comparisons?
No, it’s PHP-centric. For database operations, you’ll need to manually translate comparators to SQL or use query macros/scopes. Complex SQL (e.g., `CASE WHEN`) may still require custom logic. The package excels in application-layer comparisons, not raw SQL.
What Laravel versions and PHP versions are officially supported?
The package supports PHP 8.1+ and is designed to work with Laravel 9+. It leverages modern PHP features like enums and named arguments, ensuring compatibility with Laravel’s latest releases. Check the [documentation](https://php-standard-library.dev) for version-specific notes.
Can I use this for deep comparison of nested objects or arrays?
No, this package focuses on shallow comparisons. For deep comparison (e.g., nested objects/arrays), consider alternatives like `spatie/laravel-array-to-object` or `phpunit/phpunit`’s `assertEquals()` with recursion. This package prioritizes performance and simplicity for common use cases.
How does this handle null values in comparisons?
The package enforces strict comparison rules by default, so `null` values require explicit handling. Use methods like `isNull()` or `isNotNull()` for null checks, or extend the `Comparator` class to add custom null-aware logic. For domain objects with nullable fields, wrap comparisons in `optional()` or add adapters.
Will this break existing loose-comparison patterns in my Laravel app?
Yes, the package enforces strict comparison rules (e.g., `equals()` vs. `looseEquals()`). If your app relies on loose comparisons (e.g., `==` for type juggling), you’ll need to refactor or use the `looseEquals()` method explicitly. This trade-off ensures type safety and consistency.
How can I use this for API response sorting/filtering in Laravel?
Centralize sorting logic in a service or middleware. For example, parse a `?sort=price:desc` query string into `Comparator::desc('price')` and apply it to collections or Eloquent queries. Pair with Laravel’s `sortBy()` or custom query macros for seamless integration.
Are there performance concerns when using this for large datasets?
No, the package introduces minimal overhead. Benchmark critical paths like sorting large collections, but expect performance comparable to native PHP operators. For database operations, the bottleneck will likely be the query itself, not the comparator logic.
How do I test comparators in Laravel’s PHPUnit tests?
Test comparators like any other logic: assert expected outcomes with `assertTrue(Comparator::greaterThan($a, $b))` or `assertFalse(Comparator::equals($a, $b))`. For edge cases (e.g., `NaN`, `Infinity`, custom objects), include explicit test cases. Mock dependencies if comparators interact with external services.
What are the alternatives to this package for Laravel comparison logic?
For deep comparison, use `spatie/laravel-array-to-object` or `phpunit/phpunit`. For loose comparisons, stick with native PHP operators or `assertEquals()` with custom normalizers. If you need SQL integration, consider `spatie/laravel-query-builder` for query scopes. This package stands out for its type safety and Laravel synergy in application-layer comparisons.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi