简体   繁体   English

Laravel 完整性约束违规:1052 列 'id' in where 子句不明确

[英]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:我的意思是,有一个父母带着他的两个孩子,如下图所示: 图像1 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_registrarsstudent_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.

相关问题 完整性约束违规:1052 where子句中的列'id'不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 违反完整性约束:1052 where子句不明确的列'prof_id'Laravel - Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 如何解决完整性约束违规:1052 列 'id' in where 子句在 laravel 中不明确 - How to solve Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous in laravel Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确 - Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous 完整性约束违规:1052 列 'group_id' 在 where 子句不明确 - Integrity constraint violation: 1052 Column 'group_id' in where clause is ambiguous 违反完整性约束:1052 order子句中的“位置”列不明确 - Integrity constraint violation: 1052 Column 'position' in order clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM