mercredi 11 mars 2020

Laravel - Submitting form to database, when the form contains information from different database table

I have created booking form which uses some of the data entered into a previous 'requestForm', only some of the fields from the requestForm are used in the bookingForm, and then a few extra new fields; SurgeryDate, TheatreRoomID, patientNotes and BookingID are included in this booking form.

I am having an issue with my submit function, as I want to submit this to submit to a new bookingforms table in my database, but at present seems to be submitting to be trying to submit to the requestForms table.

To give some background on what I have done already - the purpose of the request form is to make a surgery request, which is then added to a waiting list, from the waiting list you then click one of the entries and that brings you to the booking form, with fields that appear in both the request form and booking form already having the entry complete. The user needs to be able to fill in the rest of the fields (Patient notes, surgery date, TheatreRoomID and BookingID) and click submit, then this form be stored in a bookingForm table in my database.

I have been trying to find help from various videos and tutorials on youtube but have been struggling to make any progress on this part of my project so any help or guidance is much appreciated :(

Below is my Controller code for the requestForm, which includes a function to create the bookingForm:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\RequestForm;

class RequestFormsController extends Controller
{
    public function submit(Request $request){
      $this->validate($request, [
        'requestID' => 'required',
        'requestDate' => 'required',
        'patientID' => 'required',
        'patientForename' => 'required',
        'patientSurname'=> 'required',
        'patientSex' => 'required',
        'patientDOB' => 'required',
        'surgeryType' => 'required',
        'patientUrgency' => 'required',
        'bloodGroup' => 'required'
      ]);



      // Create new Request Form
      $requestForm = new RequestForm;
      $requestForm->requestID = $request->input('requestID');
      $requestForm->requestDate = $request->input('requestDate');
      $requestForm->patientID = $request->input('patientID');
      $requestForm->patientForename = $request->input('patientForename');
      $requestForm->patientSurname = $request->input('patientSurname');
      $requestForm->patientSex = $request->input('patientSex');
      $requestForm->patientDOB = $request->input('patientDOB');
      $requestForm->surgeryType = $request->input('surgeryType');
      $requestForm->patientUrgency = $request->input('patientUrgency');
      $requestForm->bloodGroup = $request->input('bloodGroup');

      //Save Request form

      $requestForm->save();

      //redirect

      return redirect('/')->with('success', 'Request Submitted');
    }

    public function getWaitingLists(){
      $waitinglists = RequestForm::all();

      return view('waitinglist')->with('waitinglist', $waitinglists);
    }

    public function getPatientData(Request $request,$id){
    $patientDetail = RequestForm::where('patientID',$id)->first();

    return view('bookingForm')->with('patientDetail', $patientDetail);
  }

}

Below is my bookingForm view code, the $patientData->fields are fields pulled from the requestForm to save the user the hassle of reentering details already submitted in my request form:

@extends('layouts.app')
@section('content')
<h1>Booking Form</h1>
{!! Form::open(['url' => 'bookingform/submit']) !!}


    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>


    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
    
    
    </div>

    <div class="form-group">
      
      
    </div>

    <div>
      
    </div>
{!! Form::close() !!}
@endsection

Finally, Below are my routes from my web.php:

Route::get('/requestform', 'PagesController@getRequestForm');

Route::get('/waitinglist', 'RequestFormsController@getWaitingLists');


Route::post('/requestform/submit', 'RequestFormsController@submit');

Route::get('/bookingForm/show/{patientID}', 'RequestFormsController@getPatientData' );

Route::post('/bookingform/submit', 'RequestFormsController@submit');


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire