Framework-agnostic usage
The core of this package has no hard dependency on Laravel. Data::from(),
make(), toArray(), nested data, #[ArrayOf], Sometimes, Lazy and enum
casting all work with only PHP installed.
Laravel-specific behaviour is detected at runtime through
BradieTilley\Data\DataSystemContext, which caches capability checks so the hot
path stays cheap:
use BradieTilley\Data\DataSystemContext;
DataSystemContext::hasCarbon(); // is nesbot/carbon installed?
DataSystemContext::hasCollection(); // is Illuminate\Support\Collection available?
DataSystemContext::hasModel(); // is Eloquent available?
DataSystemContext::hasConfig(); // is the Laravel config() helper available?
What changes without Laravel
| Feature | With Laravel | Without Laravel |
|---|---|---|
Data::collect() | Returns Illuminate\Support\Collection | Returns a plain array |
Collection-typed properties | Hydrated to a Collection | Treated as an array |
Carbon properties | Parsed/formatted (if Carbon installed) | Left untouched unless Carbon is installed independently |
Data::from($model) | Reads Eloquent attributes | Not applicable (no Eloquent) |
#[WhenLoaded] / #[WhenCounted] / #[WhenAppended] / #[When] | Conditional model hydration (see Laravel Integration) | Ignored (attributes are Laravel-only; hydrate from arrays as usual) |
Data::paginate(...) | Wraps Laravel paginators | Throws on unknown paginator input |
Everything else — scalar/enum/nested-data hydration and serialization — is identical in both environments.
Standalone example
require 'vendor/autoload.php';
use BradieTilley\Data\Data;
class PointData extends Data
{
public function __construct(
public int $x,
public int $y,
) {}
}
$point = PointData::from(['x' => 1, 'y' => 2]);
$point->toArray(); // ['x' => 1, 'y' => 2]