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

Range Laravel Package

php-standard-library/range

Range types for PHP integer sequences with built-in iteration support. Use range objects to represent start/end bounds and step through values predictably. Part of PHP Standard Library; see docs, contribute, or report issues on GitHub.

View on GitHub
Deep Wiki
Context7

Range types for integer sequences with iteration support

Frequently asked questions about Range
How does this package help with Laravel pagination or chunked processing?
The `Range` class provides `lazy()` for memory-efficient iteration over large sequences (e.g., user IDs or order batches), ideal for Laravel’s `chunk()` or queue workers. For example, `Range::from(1, 1_000_000)->lazy()` generates values on-demand, avoiding memory overloads. Pair it with `step()` to skip values (e.g., even IDs only) without filtering.
Can I use this for dynamic query ranges in Laravel’s Query Builder?
Yes. Convert a `Range` to an array with `toArray()` and use it with `whereIn()`, e.g., `whereIn('id', Range::from(100, 200)->toArray())`. For step-based ranges (e.g., even IDs), combine with `step(2)` and `toArray()`. No native `whereInRange()` exists yet, but the package simplifies manual array generation.
Does this work with Laravel Collections? Can I map over a Range?
Absolutely. Convert a `Range` to an array or use it directly with Collection methods: `Range::from(1, 5)->map(fn($x) => User::find($x))`. For lazy evaluation, chain `lazy()` before passing to `Collection::make()`. Example: `collect(Range::from(1, 100)->lazy())->each(...)` avoids loading all values into memory.
What Laravel versions and PHP versions are supported?
This package requires **PHP 8.0+** (due to typed properties and `Range` type support). It’s framework-agnostic but designed for Laravel 8.x–10.x. Tested with Laravel’s Collections, Query Builder, and queue systems. No Laravel-specific dependencies exist, so it works alongside any Laravel version meeting the PHP requirement.
How do I handle large datasets (e.g., 1M+ records) without memory issues?
Use `Range::lazy()` to generate values on-demand during iteration. Example: `Range::from(1, 1_000_000)->lazy()->each(fn($id) => process($id))` processes one value at a time. For queue jobs, dispatch `ProcessBatch::dispatch(Range::from(1, 1_000_000)->lazy())` to avoid loading the entire range into memory.
Is there a way to parse string ranges like '100-200:2' (start-end:step) into a Range object?
Not natively, but you can extend the package. Currently, use `Range::from($start, $end, $step)` manually. For string parsing, create a helper: `Range::fromString('100-200:2')` could be added via a custom method or facade. Check the GitHub issues for community extensions.
How does this compare to PHP 8.1’s native `Range` type?
This package adds **lazy iteration** (`lazy()`) and **step control** (`step()`), which PHP’s native `Range` lacks. It’s also more Laravel-friendly with seamless integration into Collections and Query Builder. Use this package if you need lazy evaluation or step-based ranges; otherwise, PHP 8.1’s `Range` is lighter for simple cases.
Can I cache a Range object in Laravel’s cache system?
No, not directly. `Range` objects aren’t serializable by default. Workaround: Cache the **parameters** (start, end, step) and reconstruct the `Range` on retrieval. Example: `cache()->put('range_params', [1, 100, 2]);` then `Range::from(...cache()->get('range_params'))`. Avoid caching large ranges in memory.
What’s the performance impact vs. native PHP loops (e.g., `for ($i = $start; $i <= $end; $i++)`)?
Benchmarks show **<5% overhead** for eager iteration. The `lazy()` method is **faster for large ranges** (e.g., 1M+ items) because it avoids pre-allocating arrays. For small ranges (e.g., <100 items), native loops may be marginally faster. Use `lazy()` for memory efficiency and `toArray()` when performance is critical for tiny ranges.
How do I integrate this with Laravel’s queue jobs for batch processing?
Pass a lazy `Range` to your job’s constructor or use it in the `handle()` method. Example: `ProcessUsers::dispatch(Range::from(1, 1_000_000)->lazy())`. In the job, iterate with `foreach ($this->range as $id)`. This ensures only one ID is loaded into memory at a time. For chunked processing, combine with Laravel’s `chunk()` or `batch()` methods.
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.
codraw/graphviz
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