Question: Undefined variable $patients

this is my [tag:doctor.write_medical.blade.php]. i try to get patient name from patient database that i created in migration file and fill in some form so that i can save the medical records with patient_id as foreign key. but when i try to open the page, it shows Undefined variable $patients. i dont know why it become like this it shows error at
 @foreach($patients as $patient)

<!DOCTYPE html>
<html lang="en">
<head>
  @include('doctor.css')
</head>
<body>

<header>
    <div class="topbar">
      <div class="container">
        <div class="row">
          <div class="col-sm-8 text-sm">
            <div class="site-info">
              <a href="#"><span class="mai-call text-primary"></span> 0105739057</a>
              <span class="divider">|</span>
              <a href="#"><span class="mai-mail text-primary"></span> afiqaqil@gmail.com</a>
            </div>
          </div>
          <div class="col-sm-4 text-right text-sm">
            <div class="social-mini-button">
              <a href="#"><span class="mai-logo-facebook-f"></span></a>
              <a href="#"><span class="mai-logo-twitter"></span></a>
              <a href="#"><span class="mai-logo-dribbble"></span></a>
              <a href="#"><span class="mai-logo-instagram"></span></a>
            </div>
          </div>
        </div> <!-- .row -->
      </div> <!-- .container -->
    </div> <!-- .topbar -->

    <nav class="navbar navbar-expand-lg navbar-light shadow-sm">
      <div class="container">
        <a class="navbar-brand" href="#"><span class="text-primary">MyHealthCare</span>System</a>


        <div class="collapse navbar-collapse" id="navbarSupport">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item">
              <a class="nav-link" href="#">Request Access</a>
            </li>
            <li class="nav-item active">
              <a class="nav-link" href="index.html">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="about.html">About Us</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="contact.html">Contact</a>
            </li>

            @if(Route::has('login'))
            @auth

            <x-app-layout>
                </x-app-layout>

            @else
            <li class="nav-item">
              <a class="btn btn-primary ml-lg-3" href="{{route('login')}}">Login</a>
            </li>
            <li class="nav-item">
              <a class="btn btn-primary ml-lg-3" href="{{route('register')}}">Register</a>
            </li>
            @endauth
            @endif
          </ul>
        </div> <!-- .navbar-collapse -->
      </div> <!-- .container -->
    </nav>
  </header>

<div class="container-fluid page-body-wrapper">
  <div class="row">
    <div class="col-md-2">
      <div class="sidebar-sticky">
        <ul class="nav flex-column">
          <li class="nav-item active">
            <a class="nav-link" href="{{route('doctor.home')}}">
              <i class="mdi mdi-account-search"></i>
              Add Patient
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="{{ route('write_medical') }}">
              <i class="mdi mdi-download"></i>
              Write Medical Description
            </a>
          </li>
          <li class="nav-item menu-items">
            <a class="nav-link" data-toggle="collapse" href="#doctors" aria-expanded="false" aria-controls="doctors">
              <span class="menu-icon">
                <i class="mdi mdi-doctor"></i>
              </span>
              <span class="menu-title">View Patients</span>
              <i class="menu-arrow"></i>
            </a>
            <div class="collapse" id="doctors">
              <ul class="nav flex-column sub-menu">
              <li class="nav-item">
                <a class="nav-link" href="{{ route('patient_1') }}">Patient 1</a>
                </li>

                <li class="nav-item">
                  <a class="nav-link" href="{{ route('add_doctor_view') }}">Patient 2</a>
                </li>
              </ul>
            </div>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">
              <i class="mdi mdi-chat"></i>
              Chat with Patients
            </a>
          </li>
        </ul>
      </div>
    </div>

    <div class="col-md-10">
      <div class="content-wrapper">
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
              <div class="form-container">
              @if (session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif
                  <form action="{{ route('save_medical') }}" method="POST">
                  @csrf

                  <div class="form-group">
                      <label for="patient">Patient</label>
                      <select class="form-control" id="patients" name="patients" required>
                          @foreach($patients as $patient)
                              <option value="{{ $patient->patient_id }}">{{ $patient->patient_name }}</option>
                          @endforeach
                      </select>
                  </div>

                  <div class="form-group">
                    <label for="date">Date</label>
                    <input type="text" class="form-control" id="date" value="{{ date('Y-m-d') }}" readonly>
                  </div>
                  <div class="form-group">
                    <label for="symptoms">Symptoms</label>
                    <textarea class="form-control" id="symptoms" name="symptoms" rows="4" required></textarea>
                  </div>
                  <div class="form-group">
                    <label for="treatment">Simplified Treatment</label>
                    <textarea class="form-control" id="treatment" name="treatment" rows="4" required></textarea>
                  </div>
                  <div class="form-group">
                    <label for="medication">Medication</label>
                    <textarea class="form-control" id="medication" name="medication" rows="4" required></textarea>
                  </div>
                  <button type="submit" class="btn btn-primary">Submit</button>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<footer class="page-footer">
    <div class="container">
      <div class="row px-md-3">
        <div class="col-sm-6 col-lg-3 py-3">
          <h5>Company</h5>
          <ul class="footer-menu">
            <li><a href="#">About Us</a></li>
            <li><a href="#">Career</a></li>
            <li><a href="#">Editorial Team</a></li>
            <li><a href="#">Protection</a></li>
          </ul>
        </div>
        <div class="col-sm-6 col-lg-3 py-3">
          <h5>More</h5>
          <ul class="footer-menu">
            <li><a href="#">Terms & Condition</a></li>
            <li><a href="#">Privacy</a></li>
            <li><a href="#">Advertise</a></li>
            <li><a href="#">Join as Doctors</a></li>
          </ul>
        </div>
        <div class="col-sm-6 col-lg-3 py-3">
          <h5>Our partner</h5>
          <ul class="footer-menu">
            <li><a href="#">One-Fitness</a></li>
            <li><a href="#">One-Drugs</a></li>
            <li><a href="#">One-Live</a></li>
          </ul>
        </div>
        <div class="col-sm-6 col-lg-3 py-3">
          <h5>Contact</h5>
          <p class="footer-link mt-2">351 Willow Street Franklin, MA 02038</p>
          <a href="#" class="footer-link">701-573-7582</a>
          <a href="#" class="footer-link">healthcare@temporary.net</a>

          <h5 class="mt-3">Social Media</h5>
          <div class="footer-sosmed mt-3">
            <a href="#" target="_blank"><span class="mai-logo-facebook-f"></span></a>
            <a href="#" target="_blank"><span class="mai-logo-twitter"></span></a>
            <a href="#" target="_blank"><span class="mai-logo-google-plus-g"></span></a>
            <a href="#" target="_blank"><span class="mai-logo-instagram"></span></a>
            <a href="#" target="_blank"><span class="mai-logo-linkedin"></span></a>
          </div>
        </div>
      </div>

      <hr>

      <p id="copyright">Copyright &copy; 2020 <a href="https://macodeid.com/" target="_blank">MACode ID</a>. All right reserved</p>
    </div>
  </footer>

@include('doctor.script')
  
</body>
</html>


this is my route web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\RegisterController;
use App\Http\Controllers\Admin\DataRequestorController;
use App\Http\Controllers\DoctorController;
use App\Http\Controllers\PatientController;
use App\Http\Controllers\AccessRequestController;
use App\Http\Controllers\WriteMedicalController;




Route::get('access-request/create', [AccessRequestController::class, 'create'])->name('access-request.create');
Route::post('access-request', [AccessRequestController::class, 'store'])->name('access-request.store');

Route::get('/get-doctors-count', [DoctorController::class, 'getDoctorsCount'])->name('get_doctors_count');
Route::post('/add-patient', [PatientController::class, 'addPatient'])->name('add.patient');

Route::get('/write-medical', [WriteMedicalController::class, 'writeMedical'])->name('write_medical');
Route::post('/save-medical', [WriteMedicalController::class, 'saveMedical'])->name('save_medical');

Route::get('/admin/data-requestors/{requestor}/accept', [DataRequestorController::class, 'accept'])->name('admin.data-requestors.accept');
Route::get('/admin/data-requestors/{requestor}/decline', [DataRequestorController::class, 'decline'])->name('admin.data-requestors.decline');
Route::get('/admin/data-requestors/{requestor}', [DataRequestorController::class, 'show'])->name('admin.data-requestors.show');
Route::get('/admin/data-requestors', [DataRequestorController::class, 'index'])->name('admin.data-requestors.index');

Route::get('/', [HomeController::class, 'index']);
Route::get('/home', [HomeController::class, 'redirect'])->name('home.redirect');
Route::get('/doctor/home', [HomeController::class, 'doctorHome'])->name('doctor.home');
Route::get('/user/home', [HomeController::class, 'userHome'])->name('user.home');

Route::middleware([
    'auth:sanctum',
    config('jetstream.auth_session'),
    'verified'
])->group(function () {
    Route::get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');
});

Route::get('/write-medical', function () {
    return view('doctor.write_medical');
})->name('write_medical');

Route::get('/patient_1', function () {
    return view('doctor.patient_1');
})->name('patient_1');

Route::get('/add_doctor_view', [AdminController::class, 'addview'])->name('add_doctor_view');
Route::post('/register_doctor', [AdminController::class, 'registerDoctor'])->name('register.doctor');
Route::get('/viewDoctors', [AdminController::class, 'viewDoctors'])->name('viewDoctors');
Route::delete('/delete_doctor/{doctor}', [AdminController::class, 'deleteDoctor'])->name('admin.doctors.delete');

and this is my writemedicalcontroller
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\MedicalDescription;
use App\Models\Patient;
use App\Models\Doctor;

class WriteMedicalController extends Controller
{
    public function writeMedical()
    {
        $patients = Patient::all();

        return view('doctor.write_medical',  compact('patients'));
    }

    public function saveMedical(Request $request)
    {
        $validatedData = $request->validate([
            'patient' => 'required|exists:patients,patient_id',
            'symptoms' => 'required',
            'treatment' => 'required',
            'medication' => 'required',
        ]);        

        $medicalDescription = new MedicalDescription();
        $medicalDescription->patient_id = $validatedData['patient'];
        $medicalDescription->date = date('Y-m-d');
        $medicalDescription->symptoms = $validatedData['symptoms'];
        $medicalDescription->treatment = $validatedData['treatment'];
        $medicalDescription->medication = $validatedData['medication'];
        $medicalDescription->save();

        return redirect()->back()->with('success', 'Medical description saved successfully.');
    }
}

this is my medicaldescription model


Answer:
Your route web.php file has two definitions of the route /write-medical, so Laravel is using the last definition:
Route::get('/write-medical', function () {
    return view('doctor.write_medical');
})->name('write_medical');

Remove that and I think it will work as you expect because your first definition is calling the controller WriteMedicalController::writeMedical as you expect.