12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Repositories\Models\Mentor;
- use App\Repositories\Enums\Base\AdminTypeEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- class Student extends Model
- {
- /**
- * @var string
- */
- protected $table = 'mentor_students';
- protected $guarded = [];
- /**
- * The attributes excluded from the model's JSON form.
- *
- * @var array
- */
- protected $hidden = [];
- protected static function boot()
- {
- parent::boot(); // TODO: Change the autogenerated stub
- static::created(function (Student $model) {
- $admin = Admin::query()->create([
- 'username' => $model->account,
- 'password' => Hash::make($model->password),
- 'name' => $model->truename,
- 'type' => AdminTypeEnum::USER,
- 'status' => ModelStatusEnum::OK,
- 'email' => $model->email,
- 'shop_id' => $model->grade_id,
- ]);
- $admin->assignRole('student');
- DB::table('mentor_students')->where('id', $model->id)->update(['admin_id' => $admin->id]);
- });
- static::saved(function (Student $model) {
- $data = [
- 'username' => $model->account,
- 'name' => $model->truename,
- 'email' => $model->email,
- 'status' => $model->status,
- 'shop_id' => $model->grade_id,
- ];
- if ($model->isDirty('password')) {
- $data['password'] = Hash::make($model->password);
- }
- Admin::query()->where('type', AdminTypeEnum::USER)->where('id', $model->admin_id)->update($data);
- });
- static::deleted(function (Student $model) {
- Admin::query()->where('type', AdminTypeEnum::USER)->where('id', $model->admin_id)->delete();
- });
- }
- public function grade()
- {
- return $this->belongsTo(Grade::class)->select(['id', 'name']);
- }
- }
|