Student.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Repositories\Models\User;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Base\Admin;
  5. use App\Repositories\Models\Model;
  6. use Illuminate\Support\Facades\Hash;
  7. use Prettus\Repository\Contracts\Transformable;
  8. use Prettus\Repository\Traits\TransformableTrait;
  9. /**
  10. * Class Student.
  11. *
  12. * @package namespace App\Repositories\Models\User;
  13. */
  14. class Student extends Model implements Transformable
  15. {
  16. use TransformableTrait;
  17. protected $table = 'user_students';
  18. /**
  19. * The attributes that are mass assignable.
  20. *
  21. * @var array
  22. */
  23. protected $guarded = [];
  24. protected static function booted()
  25. {
  26. static::created(function (Student $student) {
  27. Admin::query()->create([
  28. 'username' => $student->account,
  29. 'password' => Hash::make($student->password),
  30. 'name' => $student->turename,
  31. 'headimg' => $student->headimg,
  32. 'type' => Student::class,
  33. 'type_id' => $student->id,
  34. 'is_admin' => 0,
  35. 'department_id' => 0,
  36. 'role_id' => 1,
  37. 'status' => ModelStatusEnum::OK,
  38. ]);
  39. });
  40. static::saved(function (Student $student) {
  41. $data = [
  42. 'username' => $student->account,
  43. 'name' => $student->turename,
  44. 'password' => Hash::make($student->password),
  45. 'headimg' => $student->headimg,
  46. ];
  47. Admin::query()->where('type_id', $student->id)->where('type', Student::class)->update($data);
  48. });
  49. static::deleted(function (Student $student) {
  50. Admin::query()->where('type_id', $student->id)->where('type', Student::class)->delete();
  51. });
  52. }
  53. }