英文:
Getting SQLSTATE[23000] error with Laravel global scope in many-to-many relationship
问题
主题 1:我在我的项目中使用 Laravel 版本 7,并为了在 Laravel 的所有模型中添加查询,我已经为所有模型添加了一个全局范围。这样,我的所有模型都继承自另一个模型。下面是提到的模型。
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
class Model extends \Illuminate\Database\Eloquent\Model
{
/**
* 模型的“启动”方法。
* @return void
*/
protected static function boot()
{
parent::boot();
if (!empty(Client::$Current_Token)) {
static::addGlobalScope('client_token', function (Builder $builder) {
$builder->where('client_token', Client::$Current_Token);
});
}
}
}
主题 2:有一个名为 "user" 的模型和一个名为 "role" 的模型,这两个表之间有一个多对多的关系。现在,想象一下,我想在 "user" 模型中使用 belongsToMany 方法检索与用户关联的所有角色,基于它们在中间表中定义的关系。
/**
* 属于用户的角色。
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
}
问题:我遇到了以下错误:SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'client_token' in the WHERE clause is ambiguous
,我知道这与我在全局范围中添加的条件有关。
英文:
Subject 1: I am using Laravel version 7 in my project, and in order to add a query to all my models in Laravel, I have added a global scope to all my models. In this way, all my models inherit from another model. The mentioned model is provided below.
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
class Model extends \Illuminate\Database\Eloquent\Model
{
/**
* The "booting" method of the model.
* @return void
*/
protected static function boot()
{
parent::boot();
if(!empty(Client::$Current_Token)) {
static::addGlobalScope('client_token', function (Builder $builder) {
$builder->where('client_token', Client::$Current_Token);
});
}
}
}
Subject 2: There is a model named "user" and a model named "role", and there is a many-to-many relationship between these 2 tables. Now, imagine that I want to retrieve all the roles associated with a user in the "user" model using the belongsToMany method, based on their relationship defined in the intermediate table.
/**
* The roles that belong to the user.
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
}
Problem: I encounter the following error: SQLSTATE\[23000\]: Integrity constraint violation: 1052 Column 'client_token' in the WHERE clause is ambiguous
and I know it is related to the condition I added in the global scope.
答案1
得分: 0
我相信你之所以收到该错误是因为你的多个表中都有client_token
列。所以当你执行涉及多个表的数据库查询时,它不知道你在谈论哪个client_token
列。
让我们创建一个作用域类,以便我们可以访问模型的表名:
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class ClientTokenScope implements Scope
{
/**
* 将作用域应用于给定的Eloquent查询构建器。
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->where("{$model->getTable()}.client_token", Client::$Current_Token);
}
}
然后在boot
方法中应用该作用域:
namespace App\Models;
use App\Scopes\ClientTokenScope;
use Illuminate\Database\Eloquent\Builder;
class Model extends \Illuminate\Database\Eloquent\Model
{
/**
* 模型的“booting”方法。
* @return void
*/
protected static function boot()
{
parent::boot();
if (!empty(Client::$Current_Token)) {
static::addGlobalScope(new ClientTokenScope);
}
}
}
英文:
I believe you got that error because a number of your tables are having that client_token
column. So when you got a database query involving multiple tables, it just doesn't know which client_token
column you are talking about.
Lets create a scope class so we can access the table name of the model:
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class ClientTokenScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->where("{$model->getTable()}.client_token", Client::$Current_Token);
}
}
Then applying the scope in the boot method:
namespace App\Models;
use App\Scopes\ClientTokenScope;
use Illuminate\Database\Eloquent\Builder;
class Model extends \Illuminate\Database\Eloquent\Model
{
/**
* The "booting" method of the model.
* @return void
*/
protected static function boot()
{
parent::boot();
if(!empty(Client::$Current_Token)) {
static::addGlobalScope(new ClientTokenScope);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论