123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Repositories\Models\School;
- use App\Repositories\Enums\Base\AdminTypeEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Base\Department;
- use App\Repositories\Models\Model;
- use Illuminate\Support\Facades\Hash;
- use Prettus\Repository\Contracts\Transformable;
- use Prettus\Repository\Traits\TransformableTrait;
- /**
- * Class Teacher.
- *
- * @package namespace App\Repositories\Models\Mentor;
- */
- class Teacher extends Model implements Transformable
- {
- use TransformableTrait;
- protected $table = 'school_teachers';
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
- protected $hidden = ['password'];
- protected static function booted()
- {
- static::created(function (Teacher $model) {
- $admin = Admin::query()->create([
- 'username' => $model->account,
- 'password' => Hash::make($model->password),
- 'name' => $model->name,
- 'headimg' => $model->headimg,
- 'type' => AdminTypeEnum::TEACHER,
- // 'type_id' => $model->id,
- 'department_id' => $model->department_id,
- 'is_admin' => 0,
- 'email' => $model->email,
- 'mobile' => $model->mobile,
- 'status' => ModelStatusEnum::OK,
- ]);
- Teacher::withoutEvents(function () use ($admin, $model) {
- $model->admin_id = $admin['id'];
- $model->save();
- });
- $admin->syncRoles(['teacher']);
- });
- static::updated(function (Teacher $model) {
- $data = [
- 'username' => $model->account,
- 'name' => $model->name,
- 'mobile' => $model->mobile,
- 'headimg' => $model->headimg,
- 'email' => $model->email,
- ];
- if ($model->isDirty('password')) {
- $data['password'] = Hash::make($model->password);
- }
- Admin::query()->where('id', $model->admin_id)->update($data);
- });
- static::updating(function (Teacher $teacher) {
- if ($teacher->mobile) {
- $teacher->is_register = 1;
- }
- });
- static::deleted(function (Teacher $model) {
- $admin = Admin::query()->where('id', $model->admin_id)->first();
- if ($admin) {
- $admin->removeRole('teacher');
- }
- 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 byIdGetUserId($id)
- {
- return Admin::query()->where('type', Teacher::class)->where('type_id', $id)->value('id') ?? 0;
- }
- public static function byUserIdGetModel($user_id)
- {
- return self::query()->where('admin_id', $user_id)->first();
- }
- }
|