Skip to content

feat: enhance pluck method to support key-value pairs and handle tabl… - #41

Open
LancelotProgrammer wants to merge 3 commits into
abdulelahragih:mainfrom
LancelotProgrammer:refactor-pluck-method
Open

feat: enhance pluck method to support key-value pairs and handle tabl…#41
LancelotProgrammer wants to merge 3 commits into
abdulelahragih:mainfrom
LancelotProgrammer:refactor-pluck-method

Conversation

@LancelotProgrammer

Copy link
Copy Markdown
Contributor

`
$productSkus = [
'019c90b9-e904-71a1-8ad7-f18336301fce',
'019c90b9-ef2e-7227-9ac3-30641cc9909e',
'019c90b9-f0bb-7045-851d-bb9882933ad2',
'019c90b9-f128-700a-a61e-91a9d0ba9b55',
];

DB::table('wishlist_items')
->where('wishlist_items.user_id', '=', '019c90b9-bb1b-7102-9f5a-c20da9e1cca4')
->whereIn('wishlist_items.product_id', $productSkus)
->join('products', 'wishlist_items.product_id', '=', 'products.id')
->where('products.product_type', '=', ProductType::DIGITAL->value)
->select('wishlist_items.product_id AS digital_product_id')
->pluck('digital_product_id');
`

SQLSTATE[42703]: Undefined column: 7 ERROR: column "digital_product_id" does not exist LINE 1: SELECT "digital_product_id" FROM "wishlist_items" INNER JOIN "...

The error happens because your custom pluck method uses $this->select($column), which completely overwrites the select clause and the alias (AS digital_product_id) you already defined earlier in your query chain. Because the alias is erased, your custom method forces the generated SQL to literally look for a physical column named digital_product_id, and since that column doesn't actually exist in your database table, PostgreSQL throws an "Undefined column" error. Laravel's native pluck method avoids this crash by using a behind-the-scenes wrapper (onceWithColumns) that safely isolates the query, extracts the raw database results, and then uses PHP to smartly parse any aliases without breaking the raw SQL syntax.

This pull request fixes the issue.

…e prefixes and add tests for various pluck scenarios

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1766a1b589

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/QueryBuilder.php

$parts = preg_split('~' . $separator . '~i', $columnString);

return end($parts);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Strip quotes from aliases in pluck column parsing

When pluck is called with an aliased expression that uses quoted identifiers (e.g. Expression::make('id AS "user_id"')), stripTableForPluck returns the alias with quotes still attached, so the subsequent $row[$column] lookup misses because PDO result keys are unquoted. In that scenario this method returns a collection of null values instead of the selected data, which is a functional regression for quoted Postgres/MySQL alias usage.

Useful? React with 👍 / 👎.

Comment thread src/QueryBuilder.php

/**
* Retrieve a single column from the result
* Get a collection of values from a given column.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants