[英]Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
[英]Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
访问关系时出现问题。 下面的语法:
$student = \App\StudentRegistrars::find($id);
foreach($student->father_registrars as $getFatherData) {
$fatherID = $getFatherData->id;
$specificFather = \App\FatherRegistrars::where('id', $fatherID);
$specificFather->update(['status' => 'Pending']);
//count qualified students who have father with id $fatherID
//problem lays here
$getSelectedStudent = \App\StudentRegistrars::where('status', 'Qualified')->whereHas('father_registrars', function($q) use($fatherID) {
$q->where('id', $fatherID);
})->count();
if($getSelectedFather == 1) {
$fatherUsername = $getFatherData->username;
$fatherCredential = \App\User::where('username', $fatherUsername);
if($fatherCredential) {
$fatherCredential->forceDelete();
}
}
}
父亲登记员
public function student_registrars(){
return $this->belongsToMany('App\StudentRegistrars')->withTrashed();
}
学生注册处
public function father_registrars(){
return $this->belongsToMany('App\FatherRegistrars')->withTrashed();
}
用户
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use HasRoles; //spatie
use SoftDeletes; //trash user
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'username', 'gender', 'phone', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
正如您在上面看到的,当我尝试计算父亲身份$fatherID
的合格学生时,问题就出现了! 我无法删除users
表中的特定记录。 它向我显示错误:完整性约束违规:1052 列 ' id
' in where 子句不明确(SQL:select 计数(*)作为来自student_registrars
的聚合,其中status
= Qualified 并且存在(select * from father_registrars
inner join father_registrars_student_registrars
on father_registrars
= father_registrars_student_registrars
father_registrars_id
其中student_registrars
deleted_at
= father_registrars_student_registrars
student_registrars
id
student_registrars_id
id
)
更新:我会更清楚地解释这个案例。 我的意思是,有一个父母带着他的两个孩子,如下图所示: 当我点击“合格”按钮时,它会自动在users
表中生成帐户,如下图所示: 一切都还好,直到这里。
但是当我点击“Hold Back”按钮时,问题就出现了。 在这种情况下我想要的是,当父母在users
表中仍然有多个合格的孩子时,系统无法删除users
表中的父母。 否则,当我点击“保留”按钮时,如果父母只有一个合格的孩子, users
表中的数据父母将被自动删除。
//count qualified students who have father with id $fatherID
//problem lays here
$getSelectedStudent = \App\StudentRegistrars::where('status', 'Qualified')->whereHas('father_registrars', function($q) use($fatherID) {
$q->where('id', $fatherID);
})->count();
在$q->where('id', $fatherID);
这里的“id”不明确,因为在您的联接查询中, father_registrars
和student_registrars
表都具有相同的列id 。因此,在您的情况下,没有表名的条件是导致不明确的原因。 用列指定表名将解决您的问题。
所以在你的情况下,你的查询应该是这样的。
$getSelectedStudent = \App\StudentRegistrars::where('status', 'Qualified')->whereHas('father_registrars', function($q) use($fatherID) {
$q->where('father_registrars.id', $fatherID);
})->count();
在你正在做的第一个 foreach 循环内......
$specificFather = \App\FatherRegistrars::where('id', $fatherID);
当你应该做类似...
$specificFather = \App\FatherRegistrars::where('id', $fatherID)->first();
或者
$specificFather = \App\FatherRegistrars::findOrFail($fatherID);
同样在第二个 foreach 中...
$fatherCredential = \App\User::where('username', $fatherUsername)->first();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.