- How does this package compare to Laravel’s built-in Collection for immutable operations?
- This package provides native immutable variants (Vector, Map, Set) with PHP 8.1+ generics, while Laravel’s Collection is mutable by default. Use it for stateful operations like caching or background jobs where immutability prevents unintended side effects. Laravel’s Collection can still be converted to/from these types via `toArray()` and `fromArray()`.
- Will this work with Laravel’s Eloquent models or query builder?
- No, this package lacks Eloquent-specific features like relationship handling or query builder hooks. You’ll need to manually convert Eloquent results to/from collections (e.g., `Vector::fromArray($users->toArray())`). For Eloquent-heavy apps, stick to Laravel’s Collection unless you need immutability.
- Does this support Laravel’s service container or binding?
- No, it’s framework-agnostic and has no Laravel-specific dependencies. You’ll need to manually bind or instantiate collections (e.g., `app()->bind(Vector::class, fn() => new Vector())`). This avoids coupling but requires DI setup.
- What Laravel versions are supported, and are there PHP version requirements?
- The package requires PHP 8.1+ (for generics) and has no Laravel-specific constraints, so it works with Laravel 9+. However, test thoroughly—Laravel’s Collection may behave differently in edge cases (e.g., circular references).
- How do I migrate from Laravel’s Collection to this package incrementally?
- Start by wrapping critical sections (e.g., `Vector::fromArray($laravelCollection->toArray())`). Use hybrid workflows like `Vector::of(...)->map(fn($item) => $item->toArray())` for transformations. Avoid full replacement until you’ve validated performance and behavior.
- Are there performance tradeoffs for immutable collections in Laravel?
- Yes—immutable operations create copies, which may impact memory in high-throughput scenarios (e.g., bulk exports). Benchmark against Laravel’s Collection or `SplFixedArray` for your use case. For read-heavy pipelines, the tradeoff is often worth it for safety.
- Can I use this for API responses or JSON serialization?
- Yes, but manually. Convert to arrays with `toArray()` before JSON encoding. Unlike Laravel’s Collection, there’s no built-in `toJson()` or API resource integration. For complex responses, consider a hybrid approach (e.g., `Response::json($vector->toArray())`).
- How does this handle testing compared to Laravel’s Collection?
- Immutable collections simplify snapshot testing—assertions like `assertEquals($expectedVector, $actualVector)` are deterministic. For Laravel, use `freeze()` to create test fixtures from Eloquent results or API responses, reducing flakiness in unit tests.
- What alternatives exist for immutable collections in Laravel?
- Consider `SplFixedArray` for mutable performance, Laravel’s Collection with `freeze()` (if using PHP 8.1+), or packages like `spatie/array-to-object`. This package stands out for its type safety and functional API but lacks Laravel-specific features.
- Is this package production-ready for Laravel apps, given its low adoption?
- Proceed with caution. The package is technically sound but unproven in Laravel ecosystems. Start with a pilot project (e.g., a non-critical module) and monitor for edge cases. Document rollback procedures to Laravel’s Collection or native arrays if issues arise.