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
! I can't delete specific record in users
table. 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)
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: Everything is okey until here.
But the problem is coming up when I hit "Hold Back" button. 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. Otherwise, when I hit "Hold Back" button, data parents in users
table will be deleted automatically if parents only has one qualified childern.
//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);
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. 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...
$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...
$fatherCredential = \App\User::where('username', $fatherUsername)->first();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.