[英]Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
I have a problem when access relationship.访问关系时出现问题。 Below the syntax:
下面的语法:
$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();
}
}
}
FatherRegistrars父亲登记员
public function student_registrars(){
return $this->belongsToMany('App\StudentRegistrars')->withTrashed();
}
StudentRegistrars学生注册处
public function father_registrars(){
return $this->belongsToMany('App\FatherRegistrars')->withTrashed();
}
User用户
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',
];
}
As you see above, the problem is coming up when I've tried to count qualified students who have father with id $fatherID
!正如您在上面看到的,当我尝试计算父亲身份
$fatherID
的合格学生时,问题就出现了! I can't delete specific record in users
table.我无法删除
users
表中的特定记录。 It show me error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select count(*) as aggregate from student_registrars
where status
= Qualified and exists (select * from father_registrars
inner join father_registrars_student_registrars
on father_registrars
. id
= father_registrars_student_registrars
. father_registrars_id
where student_registrars
. id
= father_registrars_student_registrars
. student_registrars_id
and id
= 137) and student_registrars
. deleted_at
is null)它向我显示错误:完整性约束违规: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
)
UPDATE: I will explain more clear about this case.更新:我会更清楚地解释这个案例。 I mean, there is parent with his two child, as you see in the image below:
我的意思是,有一个父母带着他的两个孩子,如下图所示:
And when I hit button "Qualified", it will generate account in
users
table automatically, as you see in the image below:当我点击“合格”按钮时,它会自动在
users
表中生成帐户,如下图所示: Everything is okey until here.
一切都还好,直到这里。
But the problem is coming up when I hit "Hold Back" button.但是当我点击“Hold Back”按钮时,问题就出现了。 Something I want in this case is, when parents still has more than one qualified children in
users
table, system cannot delete parents in users
table.在这种情况下我想要的是,当父母在
users
表中仍然有多个合格的孩子时,系统无法删除users
表中的父母。 Otherwise, when I hit "Hold Back" button, data parents in users
table will be deleted automatically if parents only has one qualified childern.否则,当我点击“保留”按钮时,如果父母只有一个合格的孩子,
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();
In $q->where('id', $fatherID);
在
$q->where('id', $fatherID);
here 'id' is ambiguous because in your join query both father_registrars
and student_registrars
table have same column id .So in your case where condition without table name is the cause of ambiguous.这里的“id”不明确,因为在您的联接查询中,
father_registrars
和student_registrars
表都具有相同的列id 。因此,在您的情况下,没有表名的条件是导致不明确的原因。 Specify the table name with the column will solve your problem.用列指定表名将解决您的问题。
So in your case, your query should be like this.所以在你的情况下,你的查询应该是这样的。
$getSelectedStudent = \App\StudentRegistrars::where('status', 'Qualified')->whereHas('father_registrars', function($q) use($fatherID) {
$q->where('father_registrars.id', $fatherID);
})->count();
Inside the first foreach loop you are doing...在你正在做的第一个 foreach 循环内......
$specificFather = \App\FatherRegistrars::where('id', $fatherID);
when you should do something like...当你应该做类似...
$specificFather = \App\FatherRegistrars::where('id', $fatherID)->first();
OR或者
$specificFather = \App\FatherRegistrars::findOrFail($fatherID);
Also in second foreach do...同样在第二个 foreach 中...
$fatherCredential = \App\User::where('username', $fatherUsername)->first();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.