feat: enhance pluck method to support key-value pairs and handle tabl… - #41
feat: enhance pluck method to support key-value pairs and handle tabl…#41LancelotProgrammer wants to merge 3 commits into
Conversation
…e prefixes and add tests for various pluck scenarios
There was a problem hiding this comment.
💡 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".
|
|
||
| $parts = preg_split('~' . $separator . '~i', $columnString); | ||
|
|
||
| return end($parts); |
There was a problem hiding this comment.
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 👍 / 👎.
|
|
||
| /** | ||
| * Retrieve a single column from the result | ||
| * Get a collection of values from a given column. |
`
$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.