hi friends,
I have 1 model Appointment.php with AppointmentModelTrait.php and i have Payment.php Model. So i need to save my model appointment but also save data in Payment table. Im try to do it but without success.
In my AppointmentModelTrait.php i have a method call savePayment.
Code
- public function savePayment(array $payment, $appointment_id) {
- $data = Payment::create([
- 'appointment_id' => $appointment_id,
- 'gateway' => $payment->payment_method,
- 'date' => new \DateTime(),
- 'amount' => $payment->total,
- 'paid' => $payment->total,
- 'created' => new \DateTime(),
- 'status' => 2
- ]);
- $data->save();
- }
And on my save method of AppointmentApiController.php
Code
- /**
- * @Route("/", methods="POST")
- * @Route("/{id}", methods="POST", requirements={"id"="\d+"})
- * @Request({"appointment": "array", "id": "int"}, csrf=true)
- */
- public function saveAction($data, $id = 0) {
- if (!$id || !$appointment = Appointment::find($id)) {
- if ($id) {
- App::abort(404, __('Appointment not found.'));
- }
- $appointment = Appointment::create();
- }
- $data['modified'] = new \DateTime();
- $appointment->save($data);
- $appointment->savePayment($data["payment"], $appointment->id);
- $appointment = Appointment::where(['id' => $appointment->id])->related('customer', 'activity', 'location', 'employee')->first();
- try {
- (new MailHelper($appointment))->frontSendMail();
- } catch (Exception $e) {
- App::abort(400, $e->getMessage());
- }
- return ['message' => 'success', 'appointment' => $appointment, 'redirect' => App::url('@bookings')];
- }
Any can said me if im ok or what im doing wrong? thanks