laravel/framework — pull request #4738 by jchamberlain (2014-06-16 19:51:20+00:00)
Title: Allow Table Prefixes in Query Builder `lists()` Method.

Currently, table prefixes on column names are removed when calling `lists()`. Thus when joining two tables which share column names, e.g., `id`, one cannot specify which `id` to select while using `lists()` without also including a `select()`. See the discussion at #1069, for example. This PR aims to fix that.

#### The Problem
Assume a table `house` with columns `id`, `owner_id`, and `name`. Assume a second table `owner` with columns `id` and `name`. The following query is an intuitive use of `lists`, but in Laravel 4.2 and earlier will cause MySQL to complain about ambiguous columns.
```php
DB::table('house')->join('owner', 'owner_id', '=', 'owner.id')->lists('house.id');
```
A more useful version getting a key/val of house ID/owner name likewise fails:
```php
DB::table('house')->join('owner', 'owner_id', '=', 'owner.id')->lists('house.id', 'owner.name');
```

#### The Hack
The current solution as described by @guilex is to include a separate `select()`, as so:
```php
DB::table('house')->join('owner', 'owner_id', '=', 'owner.id')
->select('house.id', 'owner.name')
->lists('id', 'name');
```
It's not the end of the world, but it's somewhat annoying and certainly not intuitive.

#### The Solution
The only reason the table prefix is removed is so that Laravel can access the correct keys in the resulting collection after the query is made. What I've done in this PR is basically just remove the table prefixes after making the query instead of before, so that both Laravel and the DB know which columns you're referring to. I've also made it handle `$column` and `$key` identically so that table prefixes can be used on either. Since the job of `Builder::getListSelect()` changed to be only removing the prefixes, I took the liberty of changing its name to `removeTablePrefixes`. It wasn't used by any other methods, so that shouldn't cause any trouble.