Skip to content

feat: add Query Builder value() and pluck()#10255

Open
memleakd wants to merge 1 commit into
codeigniter4:4.8from
memleakd:feat/query-builder-value-pluck
Open

feat: add Query Builder value() and pluck()#10255
memleakd wants to merge 1 commit into
codeigniter4:4.8from
memleakd:feat/query-builder-value-pluck

Conversation

@memleakd
Copy link
Copy Markdown
Contributor

Description

This PR proposes adding two small Query Builder helpers: value() and pluck().

They cover a common case where we only need one column, not a full row or result set:

$email     = $builder->where('id', 1)->value('email');
$names     = $builder->orderBy('name', 'ASC')->pluck('name');
$namesById = $builder->pluck('name', 'id');
  • value(): returns the selected column from the first matching row, or null if 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 $reset is false. They temporarily select only the requested column or columns, so existing select() 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() with get() 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:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- 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>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant