Teacher.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Teacher.
  13. *
  14. * @package namespace App\Repositories\Models\Mentor;
  15. */
  16. class Teacher extends Model implements Transformable
  17. {
  18. use TransformableTrait;
  19. protected $table = 'school_teachers';
  20. /**
  21. * The attributes that are mass assignable.
  22. *
  23. * @var array
  24. */
  25. protected $guarded = [];
  26. protected $hidden = ['password'];
  27. protected static function booted()
  28. {
  29. static::created(function (Teacher $model) {
  30. $admin = Admin::query()->create([
  31. 'username' => $model->account,
  32. 'password' => Hash::make($model->password),
  33. 'name' => $model->name,
  34. 'headimg' => $model->headimg,
  35. 'type' => AdminTypeEnum::TEACHER,
  36. // 'type_id' => $model->id,
  37. 'department_id' => $model->department_id,
  38. 'is_admin' => 0,
  39. 'email' => $model->email,
  40. 'mobile' => $model->mobile,
  41. 'status' => ModelStatusEnum::OK,
  42. ]);
  43. Teacher::withoutEvents(function () use ($admin, $model) {
  44. $model->admin_id = $admin['id'];
  45. $model->save();
  46. });
  47. $admin->syncRoles(['teacher']);
  48. });
  49. static::updated(function (Teacher $model) {
  50. $data = [
  51. 'username' => $model->account,
  52. 'name' => $model->name,
  53. 'mobile' => $model->mobile,
  54. 'headimg' => $model->headimg,
  55. 'email' => $model->email,
  56. ];
  57. if ($model->isDirty('password')) {
  58. $data['password'] = Hash::make($model->password);
  59. }
  60. Admin::query()->where('id', $model->admin_id)->update($data);
  61. });
  62. static::updating(function (Teacher $teacher) {
  63. if ($teacher->mobile) {
  64. $teacher->is_register = 1;
  65. }
  66. });
  67. static::deleted(function (Teacher $model) {
  68. $admin = Admin::query()->where('id', $model->admin_id)->first();
  69. if ($admin) {
  70. $admin->removeRole('teacher');
  71. }
  72. if (!$admin->roles()->exists()) {
  73. $admin->delete();
  74. }
  75. });
  76. }
  77. public function getHeadimgAttribute($val)
  78. {
  79. if (empty($val)) {
  80. return path_to_url('/default/headImg.png');
  81. }
  82. return path_to_url($val);
  83. }
  84. public function department()
  85. {
  86. return $this->belongsTo(Department::class)->select(['id', 'name']);
  87. }
  88. public function byIdGetUserId($id)
  89. {
  90. return Admin::query()->where('type', Teacher::class)->where('type_id', $id)->value('id') ?? 0;
  91. }
  92. public static function byUserIdGetModel($user_id)
  93. {
  94. return self::query()->where('admin_id', $user_id)->first();
  95. }
  96. }