我在访问关系时遇到问题。语法如下:
$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
自动在表中生成帐户,如下图所示:
蛊毒传说
杨__羊羊