create([ 'username' => $model->account, 'password' => Hash::make($model->password), 'name' => $model->name, 'headimg' => $model->headimg, 'grade_id' => $model->grade_id, // 'type' => Student::class, 'type' => AdminTypeEnum::STUDENT, // 'type_id' => $model->id, 'is_admin' => 0, 'email' => $model->email, 'mobile' => $model->mobile, 'status' => ModelStatusEnum::OK, ]); Student::withoutEvents(function () use ($admin, $model) { $model->admin_id = $admin['id']; $model->save(); }); $admin->syncRoles(['student']); }); static::updated(function (Student $model) { $data = [ 'username' => $model->account, 'name' => $model->name, 'mobile' => $model->mobile, 'headimg' => $model->headimg, 'email' => $model->email, 'grade_id' => $model->grade_id, ]; if ($model->isDirty('password')) { $data['password'] = Hash::make($model->password); } Admin::query()->where('id', $model->admin_id)->update($data); }); static::updating(function (Student $teacher) { if ($teacher->mobile) { $teacher->is_register = 1; } }); static::deleted(function (Student $model) { $admin = Admin::query()->where('id', $model->admin_id)->first(); if ($admin) { $admin->removeRole('student'); } if (!$admin->roles()->exists()) { $admin->delete(); } }); } public function getHeadimgAttribute($val) { if (empty($val)) { return path_to_url('/default/headImg.png'); } return path_to_url($val); } public function department() { return $this->belongsTo(Department::class)->select(['id', 'name']); } public function grade() { return $this->belongsTo(Grade::class)->select(['id', 'name']); } public static function byUserIdGetModel($user_id) { return self::query()->where('admin_id', $user_id)->first(); } }