Student.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Repositories\Models\School;
  3. use App\Repositories\Enums\Base\AdminTypeEnum;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use App\Repositories\Models\Base\Admin;
  6. use App\Repositories\Models\Base\Department;
  7. use App\Repositories\Models\Model;
  8. use Illuminate\Support\Facades\Hash;
  9. use Prettus\Repository\Contracts\Transformable;
  10. use Prettus\Repository\Traits\TransformableTrait;
  11. /**
  12. * Class Student.
  13. *
  14. * @package namespace App\Repositories\Models\School;
  15. */
  16. class Student extends Model implements Transformable
  17. {
  18. use TransformableTrait;
  19. protected $guarded = [];
  20. protected $hidden = ['password'];
  21. protected $table = 'school_students';
  22. protected static function booted()
  23. {
  24. static::created(function (Student $model) {
  25. $admin = Admin::query()->create([
  26. 'username' => $model->account,
  27. 'password' => Hash::make($model->password),
  28. 'name' => $model->name,
  29. 'headimg' => $model->headimg,
  30. 'grade_id' => $model->grade_id,
  31. // 'type' => Student::class,
  32. 'type' => AdminTypeEnum::STUDENT,
  33. // 'type_id' => $model->id,
  34. 'is_admin' => 0,
  35. 'email' => $model->email,
  36. 'mobile' => $model->mobile,
  37. 'status' => ModelStatusEnum::OK,
  38. ]);
  39. Student::withoutEvents(function () use ($admin, $model) {
  40. $model->admin_id = $admin['id'];
  41. $model->save();
  42. });
  43. $admin->syncRoles(['student']);
  44. });
  45. static::updated(function (Student $model) {
  46. $data = [
  47. 'username' => $model->account,
  48. 'name' => $model->name,
  49. 'mobile' => $model->mobile,
  50. 'headimg' => $model->headimg,
  51. 'email' => $model->email,
  52. 'grade_id' => $model->grade_id,
  53. ];
  54. if ($model->isDirty('password')) {
  55. $data['password'] = Hash::make($model->password);
  56. }
  57. Admin::query()->where('id', $model->admin_id)->update($data);
  58. });
  59. static::updating(function (Student $teacher) {
  60. if ($teacher->mobile) {
  61. $teacher->is_register = 1;
  62. }
  63. });
  64. static::deleted(function (Student $model) {
  65. $admin = Admin::query()->where('id', $model->admin_id)->first();
  66. if ($admin) {
  67. $admin->removeRole('student');
  68. }
  69. if (!$admin->roles()->exists()) {
  70. $admin->delete();
  71. }
  72. });
  73. }
  74. public function getHeadimgAttribute($val)
  75. {
  76. if (empty($val)) {
  77. return path_to_url('/default/headImg.png');
  78. }
  79. return path_to_url($val);
  80. }
  81. public function department()
  82. {
  83. return $this->belongsTo(Department::class)->select(['id', 'name']);
  84. }
  85. public function grade()
  86. {
  87. return $this->belongsTo(Grade::class)->select(['id', 'name']);
  88. }
  89. public static function byUserIdGetModel($user_id)
  90. {
  91. return self::query()->where('admin_id', $user_id)->first();
  92. }
  93. }