簡體   English   中英

Laravel 完整性約束違規:1052 列 'id' in where 子句不明確

[英]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 )

更新:我會更清楚地解釋這個案例。 我的意思是,有一個父母帶着他的兩個孩子,如下圖所示: 圖像1 當我點擊“合格”按鈕時,它會自動在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_registrarsstudent_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM