问题
I have a problem when access data who has relationship. Below this syntax is work properly:
$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
$getSelectedStudent = \App\StudentRegistrars::where('status', 'Qualified')->whereHas('father_registrars', function($q) use($fatherID) {
$q->where('father_registrars.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, there is no problem when I've tried to count qualified students who have father with id $fatherID
! I can delete specific record in users
table.
But when I change the code above to the code below, it can't do multiple delete specific record in users
table:
$get_ids = $request->ids;
$ids = explode(',', $get_ids);
//count selected qualified students (whereIn('id', $ids)) who have father with id $fatherID
$fatherUsername = \App\FatherRegistrars::select('username')->whereHas('student_registrars', function($q) use($ids) {
$q->whereIn('student_registrars.id', $ids);
})->get();
$fatherID = \App\FatherRegistrars::select('id')->whereHas('student_registrars', function($q) use($ids) {
$q->whereIn('student_registrars.id', $ids);
})->get();
//problem lays here
$getQualifiedFatherStudent = \App\StudentRegistrars::where('status', 'Qualified')->whereHas('father_registrars', function($q) use($fatherID) {
$q->whereIn('father_registrars.id', $fatherID);
})->count();
if($getQualifiedFatherStudent == 1) {
$fatherCredential = \App\User::whereIn('username', $fatherUsername);
if($fatherCredential) {
$fatherCredential->forceDelete();
}
}
This is realtionship between student registrars and father registrars table:
[CODE UPDATED] I've updated the code below, yes the code is work, but a little, it's work only on certain case, but with another case, it's doesn't work.
$get_ids = $request->ids;
$ids = explode(',', $get_ids);
$students = \App\StudentRegistrars::whereIn('id', $ids);
$students->update(['status' => 'Pending']);
$fatherUsername = \App\FatherRegistrars::select('username')->whereHas('student_registrars', function($q) use($ids) {
$q->whereIn('student_registrars.id', $ids);
})->get();
$getQualifiedFatherStudent = \App\FatherRegistrars::where('username', $fatherUsername)->whereHas('student_registrars', function($q) {
$q->where('student_registrars.status', 'Qualified');
});
foreach($getQualifiedFatherStudent as $eachResult) {
$getCount = $eachResult->count();
if($getCount == 0) {
$fatherCredential = \App\User::whereIn('username', $fatherUsername);
if($fatherCredential) {
$fatherCredential->forceDelete();
}
}
}
Something I want is:
when parents still at least has one qualified children, system cannot delete the parents in users table. Otherwise, if parents doesn't have any qualified childern, the parents in users
table will be deleted too.
For more clear, you can see the image below: As you see in this image below, Sugeng Handoyo as a father has two qualified childern, John Doe and Dave Johnson. And james Brian as a father has only one qualified childern, Diana Esmeralda. I want to delete (Hold Back) these two.
Refer to my rule, when parents still at least has one qualified children, system cannot delete the parents in users table. Otherwise, if parents doesn't have any qualified childern, the parents in users
table will be deleted too.
But I got unexpected result below:
As you see above, James Brian should be deleted from that list, becuase his qualified childern has been deleted, but he is still exist.
来源:https://stackoverflow.com/questions/62607253/laravel-count-multiple-selected-data-who-has-relationship