Home > OS >  Target class [PatientController] does not exist
Target class [PatientController] does not exist

Time:11-05

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 >
            <div >
                <input type="text" name="name"  placeholder="Name">
            </div>
            <div >
                <input type="text" name="age"  placeholder="Age">
            </div>
        </div>

        <div >
            <label  for="gender">Gender :</label>
            <select name="gender"  id="gender" required>
              
              <option value="1">Male</option>
              <option value="2">Female</option>
              <option value="3">Other</option>
            </select>
          </div>

            <div >
                <input type="text" name="Birthday"  placeholder="Birthday">
            </div>
        </div>
        <div >
            <div >
                <textarea type="text" name="Address"  placeholder="Address"></textarea>
            </div>
            <div >
                <input type="text" name="Contact"  placeholder="Contact No">
            </div>
            <div >
                <input type="text" name="Guardian"  placeholder="Guardian">
        </div>
        

        <div >
            <label  for="room">Room Choice: </label>
            <select name="room"  id="room" required>
               @for ($i = 1; $i <= 14; $i  )
               <option value="{{ $i }}">Room {{ $i }}</option>
               @endfor
            </select>
          </div>
        
 
        <div >
            <div >
                <button type="submit" >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.

CodePudding user response:

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

use App\Http\Controllers\PatientController;

Route::post('/patients', [PatientController::class, 'store'])->name('patients.store');

CodePudding user response:

  • Check the import of the class in files you used it.
  • Check the error details for better debug.
  • Check the namespace in the PatientController file.
  • Fix the format of your route to make sure it's working properly.

and don't forget to let us know when it's solved. Good luck ^_^

CodePudding user response:

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

add PatientController to your route and import PatientController as shown below

use App\Http\Controllers\PatientController;

Route::post('/patients', [PatientController::class, 'store'])->name('patients.store');
  • Related