- How does `php-standard-library/iter` improve Laravel Eloquent queries compared to `Collection::cursor()`?
- `iter()` integrates with Eloquent’s `cursor()` to enable lazy filtering, mapping, or chunking without loading all records into memory. For example, `Model::cursor()->iter()->filter(fn($m) => $m->active)` processes rows one-by-one, unlike `Collection::cursor()`, which still requires eager loading for operations like `map()`. This is ideal for large datasets in Laravel 10+ where memory efficiency is critical.
- Can I use `iter()` with Laravel’s queue system (e.g., dispatching jobs for each item in a large array)?
- Yes. Use `iter()->chunk()` to split iterables into manageable batches, then dispatch jobs for each chunk. For example, `iter($items)->chunk(100)->each(fn($chunk) => Job::dispatch($chunk))` ensures no memory overload while maintaining queue efficiency. This is especially useful for background processing in Laravel’s `app/Jobs` directory.
- What Laravel versions and PHP versions does `php-standard-library/iter` support?
- The package requires **PHP 8.1+** and is optimized for **Laravel 10+**, leveraging modern features like iterable return types and named arguments. It avoids deprecated functions, ensuring compatibility with Laravel’s latest LTS releases. For older Laravel versions (e.g., 9.x), you may need to polyfill iterable-related features manually.
- How do I install and autoload `php-standard-library/iter` in a Laravel project?
- Run `composer require php-standard-library/iter` in your project root. Laravel’s Composer autoloader will handle the rest—no additional configuration is needed. The package follows PSR-4 standards, so it integrates seamlessly with Laravel’s dependency injection. For global access, consider wrapping it in a service provider (e.g., `app('iter')`).
- Is `iter()` faster than Laravel Collections for large datasets? How should I benchmark it?
- `iter()` excels with **lazy evaluation**, reducing memory usage by 50%+ for large datasets compared to eager `Collection` operations. Benchmark using Laravel’s `Artisan` CLI with tools like `microtime(true)` or libraries such as `php-benchmark`. Test scenarios like `iter($hugeArray)->map()` vs. `collect($hugeArray)->map()` in a Laravel command to compare execution time and memory consumption.
- Can I mix `php-standard-library/iter` with Symfony’s Iterator utilities in Laravel?
- Yes, but prioritize `iter()` for Laravel-specific use cases (e.g., Eloquent, queues) due to its optimized lazy pipelines. Symfony’s `Iterator` utilities are more general-purpose and may lack Laravel’s integration points. Use `iter()` for functional iteration and Symfony’s tools for low-level iterator manipulation if needed. Both are MIT-licensed, so no legal conflicts exist.
- How do I debug a complex `iter()` pipeline in Laravel (e.g., nested filters, maps)?
- Use the `tap()` method to log intermediate states: `iter($items)->tap(fn($i) => Log::debug('Current state:', $i))->filter(...)->map(...)`. For errors, wrap operations in `try-catch` blocks and log exceptions with `Log::error()`. Avoid debugging lazy pipelines in Blade—offload complex logic to controllers or services where you can use `dd()` or Xdebug.
- Does `php-standard-library/iter` work with Blade templates? Can I use it in `@foreach` loops?
- Yes, but with caution. While you can pass iterated results to Blade (e.g., `@foreach(iter($items)->filter(...))`), avoid heavy operations in views. For example, pre-process data in a controller: `$filtered = iter($items)->filter(...)->toArray();` then pass `$filtered` to Blade. Lazy evaluation in views can obscure execution flow and impact performance.
- What are the alternatives to `php-standard-library/iter` for lazy iteration in Laravel?
- Alternatives include Laravel’s built-in `Collection::cursor()` (eager-friendly), Symfony’s `Iterator` utilities (low-level), and custom generators. However, `iter()` stands out for its **composable pipelines**, **memory efficiency**, and **Laravel-specific integrations** (e.g., Eloquent, queues). For microservices, consider **ReactPHP** for async streams, but it’s overkill for most Laravel use cases.
- How can I contribute to or report issues with `php-standard-library/iter` in a Laravel context?
- Report issues via the [GitHub Issues](https://github.com/php-standard-library/php-standard-library/issues) tab, tagging them with `laravel` or `iteration` for clarity. Contributions are welcome—follow the [CONTRIBUTING.md](https://github.com/php-standard-library/php-standard-library/blob/next/CONTRIBUTING.md) guide. For Laravel-specific enhancements (e.g., Eloquent iterators), open a discussion or PR with clear use-case examples. The team actively reviews contributions aligned with PHP Standard Library’s goals.