Pivot 中 Where 条件的问题 Table Laravel 8
Problems with Where Condition in Pivot Table Laravel 8
你好我正在做一个项目,在这部分,为了获得一个依赖下拉列表,我想获得所有附加到特定用户的 «Areas de Interesse»,通过他的 ID 得到这个,那个在支点 table.
这些是我的迁移:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
});
Schema::create('area_interesse', function (Blueprint $table) {
$table->id();
$table->string('area_interesse');
$table->string('area_interesse_ing');
$table->timestamps();
});
Schema::create('utilizador_interesse', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger("id_utilizador");
$table->foreign('id_utilizador')->references('id')->on('users')->onDelete('cascade');;
$table->unsignedBigInteger("id_interesse");
$table->foreign('id_interesse')->references('id')->on('area_interesse');
$table->timestamps();
});
还有我的模特:
class AreaInteresse extends Model
{
protected $table = "area_interesse";
public function users(){
return $this->belongsToMany(User::class, 'utilizador_interesse', 'id_interesse' , 'id_utilizador');
}
}
class User extends Authenticatable
{
public function interesses(){
return $this->belongsToMany(AreaInteresse::class, 'utilizador_interesse', 'id_utilizador', 'id_interesse');
}
}
在我的控制器中,我尝试这样做,基于我看到的每个示例,用于测试,我试图获取与 ID 为 11
的用户相关的所有 «Areas de Interesse»
$interesses = AreaInteresse::with('users')
->whereHas('users', function($q){
$q->wherePivot('id_utilizador', 11);
})
->get();
但是我收到了这个错误,我不知道为什么会这样,也不知道为什么假设我正在尝试调用一个列 «pivot»
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause' (SQL: select * from `area_interesse` where exists (select * from `users` inner join `utilizador_interesse` on `users`.`id` = `utilizador_interesse`.`id_utilizador` where `area_interesse`.`id` = `utilizador_interesse`.`id_interesse` and `pivot` = id_utilizador))
“我正在尝试获取与 ID 为 11 的用户相关的所有 «Areas de Interesse»”
这正是模型之间定义关系的原因。只需实例化用户,然后获取相关模型:
$user = User::findOrFail(11);
$interesses = $user->interesses;
如评论中所述,值得在此重复,您应该调用 class Utilizadore
而不是 User
,或者更改 table 和列名称以匹配class.
的名称
你好我正在做一个项目,在这部分,为了获得一个依赖下拉列表,我想获得所有附加到特定用户的 «Areas de Interesse»,通过他的 ID 得到这个,那个在支点 table.
这些是我的迁移:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
});
Schema::create('area_interesse', function (Blueprint $table) {
$table->id();
$table->string('area_interesse');
$table->string('area_interesse_ing');
$table->timestamps();
});
Schema::create('utilizador_interesse', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger("id_utilizador");
$table->foreign('id_utilizador')->references('id')->on('users')->onDelete('cascade');;
$table->unsignedBigInteger("id_interesse");
$table->foreign('id_interesse')->references('id')->on('area_interesse');
$table->timestamps();
});
还有我的模特:
class AreaInteresse extends Model
{
protected $table = "area_interesse";
public function users(){
return $this->belongsToMany(User::class, 'utilizador_interesse', 'id_interesse' , 'id_utilizador');
}
}
class User extends Authenticatable
{
public function interesses(){
return $this->belongsToMany(AreaInteresse::class, 'utilizador_interesse', 'id_utilizador', 'id_interesse');
}
}
在我的控制器中,我尝试这样做,基于我看到的每个示例,用于测试,我试图获取与 ID 为 11
的用户相关的所有 «Areas de Interesse»$interesses = AreaInteresse::with('users')
->whereHas('users', function($q){
$q->wherePivot('id_utilizador', 11);
})
->get();
但是我收到了这个错误,我不知道为什么会这样,也不知道为什么假设我正在尝试调用一个列 «pivot»
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause' (SQL: select * from `area_interesse` where exists (select * from `users` inner join `utilizador_interesse` on `users`.`id` = `utilizador_interesse`.`id_utilizador` where `area_interesse`.`id` = `utilizador_interesse`.`id_interesse` and `pivot` = id_utilizador))
“我正在尝试获取与 ID 为 11 的用户相关的所有 «Areas de Interesse»”
这正是模型之间定义关系的原因。只需实例化用户,然后获取相关模型:
$user = User::findOrFail(11);
$interesses = $user->interesses;
如评论中所述,值得在此重复,您应该调用 class Utilizadore
而不是 User
,或者更改 table 和列名称以匹配class.