skip to Main Content

hello i try to make a laravel project here’s my form html page and my inputs are name,age, gender, birthday, address, contact, guardian and room

<form action="{{ route('patients.store') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="row mb-3">
            <div class="col">
                <input type="text" name="name" class="form-control" placeholder="Name">
            </div>
            <div class="col">
                <input type="text" name="age" class="form-control" placeholder="Age">
            </div>
        </div>

        <div class="input-group mb-3">
            <label class="input-group-text" for="gender">Gender :</label>
            <select name="gender" class="gender" id="gender" required>
              
              <option value="1">Male</option>
              <option value="2">Female</option>
              <option value="3">Other</option>
            </select>
          </div>

            <div class="col">
                <input type="text" name="Birthday" class="form-control" placeholder="Birthday">
            </div>
        </div>
        <div class="row mb-3">
            <div class="col">
                <textarea type="text" name="Address" class="form-control" placeholder="Address"></textarea>
            </div>
            <div class="col">
                <input type="text" name="Contact" class="form-control" placeholder="Contact No">
            </div>
            <div class="col">
                <input type="text" name="Guardian" class="form-control" placeholder="Guardian">
        </div>
        

        <div class="input-group mb-3">
            <label class="input-group-text" for="room">Room Choice: </label>
            <select name="room" class="room" id="room" required>
               @for ($i = 1; $i <= 14; $i++)
               <option value="{{ $i }}">Room {{ $i }}</option>
               @endfor
            </select>
          </div>
        
 
        <div class="row">
            <div class="mx-5">
                <button type="submit" class="btn btn-primary my-3 mx-2">Submit</button>
            </div>
        </div>
    </form>

and this is my PatientController

public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string',
            'age' => 'required|integer',
            'gender' => 'required|string',
            'Birthday' => 'required|date',
            'Address' => 'required|string',
            'Contact' => 'required|string',
            'Guardian' => 'required|string',
            'room' => 'required|integer', // Ensure the 'room' field is validated
            'is_discharged' => 'boolean',
        ]);
        $data['is_discharged'] = $request->has('is_discharged');

        // $roomAvailability = Patient::checkRoomAvailability($data['room']);

        // if (!$roomAvailability) {
        // return back()->with('error', 'Room is full. Please choose another room.');
        // }

        $patient = Patient::create($data);

        return redirect()->route('patients.show', $patient->id)->with('success', 'Patient added Successfully');;
        }

this is the Patient Model

class Patient extends Model
{
    use HasFactory;

    protected $table = 'patients';
    protected $fillable = [
        'name',
        'age',
        'gender',
        'Birthday',
        'Address',
        'Contact',
        'Guardian',
        'room',
        'is_discharged'
    ];

    
}

and this is my route

Route::post('store', 'store')->name('patients.store');

and then When I click the submit button, it says the target class [PatientController] does not exist. how to fix this error

When you fill out the create page, it will store it in the database.

2

Answers


  1. I think what you want is to change your route to:

    use AppHttpControllersPatientController;
    
    Route::post('/patients', [PatientController::class, 'store'])->name('patients.store');
    
    Login or Signup to reply.
  2. Route::post('store', 'store')->name('patients.store');
    

    add PatientController to your route and import PatientController as shown below

    use AppHttpControllersPatientController;
    
    Route::post('/patients', [PatientController::class, 'store'])->name('patients.store');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search