-
Notifications
You must be signed in to change notification settings - Fork 11.8k
[12.x] Add AsBinary castable class #58254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I believe this is ready for a final review. The scope is intentionally minimal and fully opt-in. The implementation introduces a generic binary codec primitive, a corresponding Eloquent cast, and clear semantics around required vs optional attributes, without imposing schema or identifier strategy changes. Unless there are objections or additional edge cases to cover, this should be ready to merge. |
|
Thanks @taylorotwell for accepting the merge request. I noticed that the PHPStan generic annotation Could you share the reasoning behind this change? From my perspective, keeping this information could be beneficial for static analysis — for example, tools like IDE helpers or Larastan could infer more precise types from it. I’m curious to understand the trade-offs that led to this decision. |
|
Is this feature going to appear and be explained in the documentation? |
|
@smares Do you mean the casts table or the model keys section? |
|
The AsBinary cast what this MR implemented |
|
@smares I think so, but I would suggest documenting it once we introduce full, first-class support for binary primary and foreign keys. At that point, the documentation can clearly describe the complete, end-to-end experience rather than just the isolated Here is the roadmap: #51605 (reply in thread) |
first-class support for binary-stored identifiers in Laravel while preserving a string-based API in application code.
What’s included
AsBinaryEloquent castTransparent binary ↔ string conversion with an explicit required / optional contract.
BinaryCodecutilityCentralized, pluggable encode/decode registry.
Built-in codecs
uuidulidis_binary()helperDetects raw binary vs UTF-8 strings.
Why
Binary storage of UUID/ULID provides smaller indexes and better performance, but Laravel lacks a native, generic solution. This PR adds a framework-level primitive, not a UUID-specific abstraction.
How it works
Eloquent cast
Values are stored as binary bytes
Values are exposed as strings in PHP
The
isRequiredflag:This allows the cast definition itself to describe whether an attribute is logically required, not just how it is encoded.
Custom codecs
When to use
Recommended for:
Guarantees