Fix foreign key constraint issue

```php
public function switchStudent(Request $req)
 {
 $validator = Validator::make($req->all(), [
 'from\_class\_id' => 'required',
 'to\_class\_id' => 'required',
 'user\_id' => 'required',
 'status' => 'required|string|in:approved,pending',
 'trigger\_email' => 'required|boolean',
 ]);

 if ($validator->fails()) {
 return $this->sendError('Validation Error.', $validator->messages(), 422);
 }

 try {
 DB::beginTransaction();

 $errors = [];

 $user\_id = $req->input('user\_id');
 $from\_class\_id = $req->input('from\_class\_id');
 $to\_class\_id = $req->input('to\_class\_id');

 // Validations Start
 $user = User::where('user\_id', '=', $user\_id)->first();
 if ($user == null) {
 array\_push($errors, 'User not found.');
 return $errors;
 }

 $user\_class = UserOfClass::where('user\_id', $user\_id)->where('class\_id', $from\_class\_id)->first();
 if ($user\_class == null) {
 array\_push($errors, 'User is not from originating class.');
 return $errors;
 }

 $uoc = UserOfClass::where([['user\_id', '=', $user\_id], ['class\_id', '=', $from\_class\_id]])->first();
 $from\_transaction = Transaction::where('transaction\_id', '=', $uoc->transaction\_id)->orderBy('transaction\_id', 'DESC')->first();

 if ($from\_transaction == null) {
 array\_push($errors, 'No transaction found for originating class.');
 return $errors;
 }

 $to\_class = ClassInfo::where('class\_id', '=', $to\_class\_id)->first();

 if ($to\_class->isCapacityFull()) {
 array\_push($errors, 'Destination class is full.');
 return $errors;
 }

 if ($from\_transaction->currency\_id != $to\_class->classPrice->currency\_id && $to\_class->classPrice->price != 0) {
 array\_push($errors, 'Transaction and the class have different currencies.');
 return $errors;
 }
 // Validations End

 $total\_from\_transaction\_price = $from\_transaction->user\_paid; // + $from\_transaction->discount + $from\_transaction->special\_discount;

 $data = [];

 if ($total\_from\_transaction\_price == $to\_class->classPrice->price) {
 // Same Price
 $data['mail\_type'] = 'class\_switch\_success';
 $data['class'] = $to\_class;
 $trigger = EmailTrigger::where('trigger\_key', 'class\_switch\_success')->first();
 if (!empty($user) && $req->get('trigger\_email'))
 event(new SendEmail($trigger, $user, NULL, $data));
 } elseif ($total\_from\_transaction\_price > $to\_class->classPrice->price) {
 // Cheaper class
 $data['mail\_type'] = 'class\_switch\_success';
 $data['class'] = $to\_class;
 $trigger = EmailTrigger::where('trigger\_key', 'class\_switch\_success')->first();
 if (!empty($user) && $req->get('trigger\_email'))
 event(new SendEmail($trigger, $user, NULL, $data));
 } elseif ($total\_from\_transaction\_price < $to\_class->classPrice->price) {
 // More expensive class
 $data['mail\_type'] = 'class\_switch\_pending';
 $data['class'] = $to\_class;
 $trigger = EmailTrigger::where('trigger\_key', 'class\_switch\_pending')->first();
 if (!empty($user) && $req->get('trigger\_email'))
 event(new SendEmail($trigger, $user, NULL, $data));
 }

 // update old class to canceled
 $user\_class->status = 'canceled';
 $user\_class->update();

 // Create a new class
 $new\_class = $user\_class->replicate();
 $new\_class->class\_id = $to\_class\_id;
 $new\_class->is\_active = $req->get('status') === 'approved';
 $new\_class->status = $req->get('status');
 $new\_class->save();

 $from\_transaction->purchase\_object = $to\_class\_id;
 $from\_transaction->save();

 $switch = new ClassSwitchLog();
 $switch->from\_class\_id = $from\_class\_id;
 $switch->to\_class\_id = $to\_class\_id;
 $switch->user\_id = $user\_id;
 $switch->save();

 DB::commit();

 return $errors;
 } catch (\Exception $e) {
 DB::rollback();
 abort(500, $e);
 }
 }
```
IX 외래 키 제약 조건 문제```php공개 함수 switchStudent(요청 $req) { validator = Validator::make($req->all(), [ 'from\_class\_id' => 'required', 'to\_class\_id' => 'required', 'user\_id' => 'required', 'status' => '필수|스트링|in:승인,보류', '트리거\_이메일' => '필수|부울', ]); if ($validator->fails()) { return $this->sendError('유효성 검사 오류.', $validator->messages(), 422); } try { DB::beginTransaction(); errors = []; user\_id = $req->input('user\_id'); from\_class\_id = $req->input('from\_class\_id'); to\_class\_id = $req->input('to\_class\_id'); // 유효성 검사 시작 user = User::where('user\_id', '=', $user\_id)->first(); if ($user == null) { array\_push($errors, '사용자를 찾을 수 없습니다.'); return $errors; } user\_class = UserOfClass::where('user\_id', $user\_id)->where('class\_id', $from\_class\_id)->first(); if ($user\_class == null) { array\_push($errors, '사용자는 원래 클래스에서 온 사용자가 아닙니다.'); 반환 $errors; } uoc = UserOfClass::where([['user\_id', '=', $user\_id], ['class\_id', '=', $from\_class\_id]])->first(); from\_transaction = Transaction::where('transaction\_id', '=', $uoc->transaction\_id)->orderBy('transaction\_id', 'DESC')->first(); if ($from\_transaction == null) { array\_push($errors, '원본 클래스에 대한 트랜잭션을 찾을 수 없습니다.'); return $errors; } to\_class = ClassInfo::where('class\_id', '=', $to\_class\_id)->first(); if ($to\_class->isCapacityFull()) { array\_push($errors, '대상 클래스가 꽉 찼습니다.'); 반환 $ 오류; } if ($from\_transaction->currency\_id != $to\_class->classPrice->currency\_id && $to\_class->classPrice->price != 0) { array\_push($errors, '트랜잭션과 클래스의 통화가 다릅니다.'); 반환 $ 오류; } // 유효성 검사 종료 total\_from\_transaction\_price = $from\_transaction->user\_paid; // + $from\_transaction->discount + $from\_transaction->special\_discount; 데이터 = []; if ($total\_from\_transaction\_price == $to\_class->classPrice->price) { // 동일한 가격 data['mail\_type'] = 'class\_switch\_success'; 데이터['클래스'] = $to\_class; trigger = EmailTrigger::where('trigger\_key', 'class\_switch\_success')->first(); if (!empty($user) && $req->get('trigger\_email')) event(new SendEmail($trigger, $user, NULL, $data)); } elseif ($total\_from\_transaction\_price > $to\_class->classPrice->price) { // 더 저렴한 클래스 data['mail\_type'] = 'class\_switch\_success'; 데이터['클래스'] = $to\_class; trigger = EmailTrigger::where('trigger\_key', 'class\_switch\_success')->first(); if (!empty($user) && $req->get('trigger\_email')) event(new SendEmail($trigger, $user, NULL, $data)); } elseif ($total\_from\_transaction\_price < $to\_class->classPrice->price) { // 더 비싼 클래스 data['mail\_type'] = 'class\_switch\_pending'; 데이터['클래스'] = $to\_class; trigger = EmailTrigger::where('trigger\_key', 'class\_switch\_pending')->first(); if (!empty($user) && $req->get('trigger\_email')) event(new SendEmail($trigger, $user, NULL, $data)); } // 이전 클래스를 취소됨으로 업데이트 user\_class->status = '취소됨'; user\_class->update(); // 새 클래스 생성 new\_class = $user\_class->replicate(); new\_class->class\_id = $to\_class\_id; new\_class->is\_active = $req->get('status') === '승인됨'; new\_class->status = $req->get('status'); new\_class->save(); from\_transaction->purchase\_object = $to\_class\_id; from\_transaction->save(); 스위치 = 새 클래스스위치로그(); switch->from\_class\_id = $from\_class\_id; switch->to\_class\_id = $to\_class\_id; switch->user\_id = $user\_id; switch->save(); DB::commit(); 반환 $error; } catch (\Exception $e) { DB::롤백(); abort(500, $e); } }```