skip to Main Content

I ran into the error:

ErrorException Undefined variable: pesan

I have gone through similar problems solved here but none of the solutions have worked for me.

The error is thrown from my RegisterController at bottom of the line. At the pesan

I use laravel 8

Here is my controller code:

` public function simpan_rm(Request $request)
{

    $this->validate($request, [
        'idpasien' => 'required|numeric|digits_between:1,4',
        'keluhan_utama' => 'required|max:40',
        'anamnesis' => 'required|max:1000',
        'px_fisik' => 'max:1000',
        'diagnosis' => 'max:40',
        'dokter' => 'required',
    ]);
   // Decoding array input pemeriksaan lab
   if (isset($request->lab))
   {
        if (has_dupes(array_column($request->lab,'id'))){
            $errors = new MessageBag(['lab'=>['Lab yang sama tidak boleh dimasukan berulang']]);
            return back()->withErrors($errors);
        }
        $this->validate($request, [
            'lab.*.hasil' => 'required|numeric|digits_between:1,4',          
        ]);
        $lab_id = decode('lab','id',$request->lab);
        $lab_hasil = decode('lab','hasil',$request->lab);
   }
   else {
    $lab_id ="";
    $lab_hasil ="";
   }

   // Decoding array input resep
   if (isset($request->resep))
    {
        if (has_dupes(array_column($request->resep,'id'))){
            $errors = new MessageBag(['resep'=>['resep yang sama tidak boleh dimasukan berulang']]);
            return back()->withErrors($errors);
        }
        $this->validate($request, [
            'resep.*.jumlah' => 'required|numeric|digits_between:1,3',
            'resep.*.aturan' => 'required',
        ]);
        $resep_id = decode('resep','id',$request->resep);
        $resep_jumlah = decode('resep','jumlah',$request->resep);
        $resep_dosis = decode('resep','aturan',$request->resep); 
    }
    else {
        $resep_id = "";
        $resep_jumlah = "";
        $resep_dosis = "";
    }
    $newresep = array();
    $oldresep=array();
    if (is_array($request) || is_object($newresep))
    {
    foreach ($request->resep as $resep){
        $newresep[$resep['id']] = $resep['jumlah'];
    }
    }
    if (empty($oldresep)) {
        $resultanresep = resultan_resep($oldresep,$newresep);
    }
    else {$resultanresep=$newresep;}
    $errors = validasi_stok($resultanresep);
    if ($errors !== NULL) {
      return  back()->withErrors($errors);
    }

    foreach ($resultanresep as $key => $value) {
        $perintah=kurangi_stok($key,$value);
        if ($perintah === false) { $habis = array_push($habis,$key); }
    }

    DB::table('rm')->insert([
        'idpasien' => $request->idpasien,
        'ku' => $request->keluhan_utama,
        'anamnesis' => $request->anamnesis,
        'pxfisik' => $request->px_fisik,
        'lab' => $lab_id,
        'hasil' => $lab_hasil,
        'diagnosis' => $request->diagnosis,
        'resep' => $resep_id,
        'jumlah' => $resep_jumlah,
        'aturan' => $resep_dosis,
        'dokter' => $request->dokter,
        'created_time' => Carbon::now(),
        'updated_time' => Carbon::now(),
    ]);

       $ids= DB::table('rm')->latest('created_time')->first();         
        switch($request->simpan) {
            case 'simpan_edit': 
                $buka=route('rm.edit',$ids->id);
                $pesan='Data Rekam Medis berhasil disimpan!';
            break;             
            case 'simpan_baru': 
                $buka=route('rm.tambah.id',$request->idpasien);;
                $pesan='Data Rekam Medis berhasil disimpan!';
            break;
        }
   
     return redirect('buka')->with('pesan',$pesan);  
     
}`

Unfortunately i’ve modified return redirect('buka')->with('pesan',$pesan); but didn’t work for me.
Please help

2

Answers


  1. $pesan is defined in 2 cases of your switch statement.

    So what happens when $request->simpan is neither equal to simpan_edit or simpan_baru ?

    In this case (wich is not handled in your code), $pesan will be undefined and an error will throw.

    A good practice using switch statements is to add a default: case wich will match any other values that you didn’t explicitely set a case: for.

    Example :

    switch($request->simpan) {
        case 'simpan_edit': 
            $buka=route('rm.edit',$ids->id);
            $pesan='Data Rekam Medis berhasil disimpan!';
            break;             
        case 'simpan_baru': 
            $buka=route('rm.tambah.id',$request->idpasien);;
            $pesan='Data Rekam Medis berhasil disimpan!';
            break;
        default:
            // you may set a default value for `$pesan` here
            $pesan=null;                
    }
    
    Login or Signup to reply.
  2. it seems that the error is related to the $pesan variable, according to the code you’ve provided, $pesan is not defined anywhere in the controller, which is why you’re receiving the Error Exception Undefined variable : pesan error
    for resolve this issue, you should either define the $pesan variable or remove any reference to it in the code
    if $pesan is meant to store some message, it should be defined and assigned a value before being used

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search