avris/bag
avris/bag is a small Laravel/PHP utility package providing a “bag” style container for working with grouped values and simple data access. Handy for passing around structured payloads, storing arbitrary attributes, and keeping collections of data lightweight.
avris/bag package provides a fluent, collection-like interface for PHP arrays, offering methods such as get(), set(), merge(), filter(), and map(). This aligns well with applications requiring:
bag(['a', 'b'])->filter(fn($v) => $v !== 'a') vs. array_filter([...])).Collection is overkill.Collection, reducing cognitive load for teams familiar with Laravel. Could complement Laravel apps where native arrays are used heavily (e.g., legacy codebases or performance-critical paths).Illuminate\Support\Collection (more features, heavier).spatie/array (simpler, fewer methods).league/collection (more enterprise-focused).
bag sits in the "sweet spot" for teams wanting Laravel-like syntax without Laravel’s dependencies.bag()->toArray()), easing adoption.Collection features (e.g., groupBy, pluck, diff). May require fallbacks to native arrays or other packages.spatie/array).Collection for performance or memory reasons?diff, collapse) force workarounds or migration to another package?Collection in non-DI contexts (e.g., services, commands).ArrayCollection.league/collection or Laravel Collection instead).bag.get, filter, map) to validate syntax benefits.set, merge, push) if immutability is desired.bag()->toArray() where native arrays are required (e.g., for APIs, databases).bag() as a factory method in constructors:
public function __construct() {
$this->data = bag(['key' => 'value']);
}
bag() usage for arrays in new code.Collection unless necessary (e.g., use bag()->toCollection() for interop).ArrayCollection, but prefer one or the other to avoid confusion.bag()->all() or bag()->keys() provide drop-in replacements for array_* functions.ArrayAccess or __toString() to ensure expected behavior.bag methods in internal codebases.array_* calls improve readability and maintainability.Bag methods instead of native arrays, requiring familiarity with the package.bag usage patterns in the codebase.Bag serialization problem").bag vs. native array usage.Bag methods in logs (e.g., Method 'nonexistent' not found).array_diff for diff operations").bag()->merge()) may create copies, but this is true of native arrays too.bag.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Package abandonment | No updates, security risks | Fork or migrate to spatie/array if critical. |
| Method incompatibilities | Runtime errors in production | Feature flags for bag usage. |
| Performance regressions | Slow array operations | Benchmark before/after adoption. |
| Type-related bugs | Undefined key access crashes | Use bag()->has() or bag()->get($key, null). |
| Serialization issues | JSON/API failures | Ensure toArray() is called before output. |
bag vs. native arrays, with live refactoring examples.bag()->pluck('key')->filter(...)).bag over time.bag reduce cognitive load?").bag globally if needed.bag for quick revert.How can I help you explore Laravel packages today?