laravel/framework — pull request #5461 by kevdowney (2014-08-14 22:27:01+00:00)
Title: PDOException: SQLSTATE[HY000]: General error: 1 ambiguous column name: id

Global scopes that join tables will break the Model::find() methods.

Make Builder::find and Builder::findMany use the fully qualified names so global scopes can still work.

Setup a global scope that will join a table:
```php
trait AccountAttachedTrait {

/**
* Boot the account attached trait for a model.
*
* @return void
*/
public static function bootAccountAttachedTrait()
{
static::addGlobalScope(new AccountAttachedScope);
}

}
```

```php
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Builder;

class AccountAttachedScope implements ScopeInterface {

/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
public function apply(Builder $builder)
{
$model = $builder->getModel();

// join the accounts table
$builder->join('accounts', $model->getTable().'.account_id', '=', 'accounts.id');

// Add account Id to the query
$builder->where($model->getTable().'.account_id', '=', \Account::getId()); //= 1
}

/**
* Remove the scope from the given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
public function remove(Builder $builder)
{
$column = $builder->getModel()->getTable().'.account_id';

$query = $builder->getQuery();

foreach ((array) $query->wheres as $key => $where)
{
// If the where clause is an account constraint, we will remove it from
// the query and reset the keys on the wheres. This allows this developer to
// include account attached model in a relationship result set that is lazy loaded.
if ($this->isAccountConstraint($where, $column))
{
unset($query->wheres[$key]);

$query->wheres = array_values($query->wheres);
}
}
}

/**
* Determine if the given where clause is an account constraint.
*
* @param array $where
* @param string $column
* @return bool
*/
protected function isAccountConstraint(array $where, $column)
{
return $where['type'] === 'Basic' && $where['column'] === $column && $where['operator'] === '=';
}
}
```

Make Product model use global account scope:
```php
class Product extends BaseSoftModel
{
use AccountAttachedTrait;

protected $table = 'products';
...
}
```

Now attempt to call find method:
`$product = Product::find('1');`

```php
PDOException: SQLSTATE[HY000]: General error: 1 ambiguous column name: id

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 ambiguous column name: id (SQL: select * from "products" inner join "accounts" on "products"."account_id" = "accounts"."id" where "products"."account_id" = 1 and "products"."deleted_at" is null and "id" = 1 limit 1)

/vendor/laravel/framework/src/Illuminate/Database/Connection.php:600
/vendor/laravel/framework/src/Illuminate/Database/Connection.php:556
/vendor/laravel/framework/src/Illuminate/Database/Connection.php:290
/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1359
/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1349
/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1336
/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:415
/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:151
/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:125
/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:82
/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:644
/app/tests/models/ProductTest.php:108

Caused by
PDOException: SQLSTATE[HY000]: General error: 1 ambiguous column name: id
```