feat: add Query Builder value() and pluck()#10255
Conversation
- add value() for retrieving one scalar column value - add pluck() for retrieving one column as a list or keyed array - expose Model wrappers that preserve soft-delete behavior - document reset, select replacement, and raw SQL limitations - add builder, live database, and model coverage Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
|
I'm not convinced these helpers belong in the core API. The Model wrappers are also questionable. Our current implementation already provides I haven't looked into this in detail yet, but let's see what others have to say. |
|
Thanks, that's a very fair concern. I still think there is real value in having these at the Query Builder level. In everyday app code, needing "just one value" or "this one column as an array" comes up pretty often, and right now it takes a few small but repetitive steps through I also agree that the Model side is a different story. The overlap with So for me the main value here is really the Builder DX: simple, explicit, easy-to-discover helpers for common scalar/list reads. I'll wait for more feedback before changing anything, but I'm open to narrowing this to Builder-only if that feels like the right boundary. And if the team feels this does not fit the core Builder API either, I'm also okay closing it. |
Description
This PR proposes adding two small Query Builder helpers:
value()andpluck().They cover a common case where we only need one column, not a full row or result set:
value(): returns the selected column from the first matching row, ornullif nothing matches.pluck(): returns one column as an array, and can optionally use another column as the array key.These methods behave like other terminal Query Builder methods: they use the current builder state, reset it by default, and can keep it when
$resetisfalse. They temporarily select only the requested column or columns, so existingselect()clauses are not used.I also added Model wrappers so soft-delete behavior is preserved when calling these through a model.
Raw SQL expressions are intentionally not accepted as column names. If users need an expression,
select()withget()remains the right tool.Tests cover SQL generation, reset behavior, query failures, live results, keyed pluck results, invalid column input, and Model soft deletes.
Checklist: