Student.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Database\Eloquent\Builder;
  7. use Illuminate\Support\Facades\App;
  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\User;
  15. */
  16. class Student extends Model implements Transformable
  17. {
  18. use TransformableTrait;
  19. protected $table = 'user_students';
  20. /**
  21. * The attributes that are mass assignable.
  22. *
  23. * @var array
  24. */
  25. protected $guarded = [];
  26. protected static function booted()
  27. {
  28. self::language();
  29. static::created(function (Student $student) {
  30. Admin::query()->create([
  31. 'username' => $student->account,
  32. 'password' => Hash::make($student->password),
  33. 'name' => $student->turename,
  34. 'headimg' => $student->headimg,
  35. 'type' => Student::class,
  36. 'type_id' => $student->id,
  37. 'is_admin' => 0,
  38. 'department_id' => 0,
  39. 'role_id' => 1,
  40. 'status' => ModelStatusEnum::OK,
  41. ]);
  42. });
  43. static::saved(function (Student $student) {
  44. $data = [
  45. 'username' => $student->account,
  46. 'name' => $student->turename,
  47. 'password' => Hash::make($student->password),
  48. 'headimg' => $student->headimg,
  49. ];
  50. Admin::query()->where('type_id', $student->id)->where('type', Student::class)->update($data);
  51. });
  52. static::deleted(function (Student $student) {
  53. Admin::query()->where('type_id', $student->id)->where('type', Student::class)->delete();
  54. });
  55. }
  56. }