亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 完整性約束違規:1052 where 子句中的列“id”不明確

Laravel 完整性約束違規:1052 where 子句中的列“id”不明確

PHP
小唯快跑啊 2023-06-24 18:39:31
我在訪問關系時遇到問題。語法如下:$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',    ]; 更新: 我會更清楚地解釋這個案例。我的意思是,有一個父母帶著他的兩個孩子,如下圖所示:當我點擊“合格”按鈕時,它將users自動在表中生成帳戶,如下圖所示:
查看完整描述

2 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

//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();


查看完整回答
反對 回復 2023-06-24
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

在您正在執行的第一個 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();


查看完整回答
反對 回復 2023-06-24
  • 2 回答
  • 0 關注
  • 187 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號