Laravel 全局范围和连接
Laravel global scopes and joins
我在 Laravel 5.1 中设置了全局范围,工作正常。但是在我的一些页面上,我使用 MySQL joins
使用 Eloquent 构建器。这会导致模棱两可的错误:
Column 'cust_id' in where clause is ambiguous
我不确定如何避免这个问题。我知道我可以使用子查询来代替,但是没有其他解决方案吗?
这是我的范围文件:
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ScopeInterface;
use App\Customers;
use DB, Session;
class MultiTenantScope implements ScopeInterface
{
/**
* Create a new filter instance.
*
* @param UsersRoles $roles
* @return void
*/
public function __construct()
{
$this->custId = Session::get('cust_id');
}
/**
* Apply scope on the query.
*
* @param Builder $builder
* @param Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
if($this->custId)
{
$builder->whereCustId($this->custId);
}
else
{
$model = $builder->getModel();
$builder->whereNull($model->getKeyName());
}
}
/**
* Remove scope from the query.
*
* @param Builder $builder
* @param Model $model
* @return void
*/
public function remove(Builder $builder, Model $model)
{
$query = $builder->getQuery();
$query->wheres = collect($query->wheres)->reject(function ($where)
{
return ($where['column'] == 'cust_id');
})->values()->all();
}
}
为了消除该字段的歧义,您应该添加 table 名称作为前缀:
public function apply(Builder $builder, Model $model)
{
if($this->custId)
{
$fullColumnName = $model->getTable() . ".cust_id";
$builder->where($fullColumnName, $this->custId);
}
else
{
$model = $builder->getModel();
$builder->whereNull($model->getKeyName());
}
}
我在 Laravel 5.1 中设置了全局范围,工作正常。但是在我的一些页面上,我使用 MySQL joins
使用 Eloquent 构建器。这会导致模棱两可的错误:
Column 'cust_id' in where clause is ambiguous
我不确定如何避免这个问题。我知道我可以使用子查询来代替,但是没有其他解决方案吗?
这是我的范围文件:
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ScopeInterface;
use App\Customers;
use DB, Session;
class MultiTenantScope implements ScopeInterface
{
/**
* Create a new filter instance.
*
* @param UsersRoles $roles
* @return void
*/
public function __construct()
{
$this->custId = Session::get('cust_id');
}
/**
* Apply scope on the query.
*
* @param Builder $builder
* @param Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
if($this->custId)
{
$builder->whereCustId($this->custId);
}
else
{
$model = $builder->getModel();
$builder->whereNull($model->getKeyName());
}
}
/**
* Remove scope from the query.
*
* @param Builder $builder
* @param Model $model
* @return void
*/
public function remove(Builder $builder, Model $model)
{
$query = $builder->getQuery();
$query->wheres = collect($query->wheres)->reject(function ($where)
{
return ($where['column'] == 'cust_id');
})->values()->all();
}
}
为了消除该字段的歧义,您应该添加 table 名称作为前缀:
public function apply(Builder $builder, Model $model)
{
if($this->custId)
{
$fullColumnName = $model->getTable() . ".cust_id";
$builder->where($fullColumnName, $this->custId);
}
else
{
$model = $builder->getModel();
$builder->whereNull($model->getKeyName());
}
}