0
votes

i have failed to save update data, the notification error message is SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'status_member' cannot be null (SQL: update daftar_pelanggans set alamat = gdsgzddgrrse, status_member = ?, daftar_pelanggans.updated_at = 2020-12-06 03:36:35 where id = 6)

this is the button edit

 <button data-toggle="modal" data-target="#editModal-{{ $pelanggan->id }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></button>

this is the modal view

     @foreach($daftar_pelanggan as $pelanggan)
     <div class="modal fade" id="editModal-{{ $pelanggan->id }}" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
       <div class="modal-dialog modal-dialog-centered" role="document">
          <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title mb-0" id="editModalLabel">Update Data Pelanggan</h5>
                </div>
              <div class="modal-body">
                 <!-- Card body -->
                <form role="form" action="{{ route('daftar_pelanggan.update', $pelanggan->id) }}" method="POST" id="editForm">
                  @csrf
                  @method('PUT')
                    <!-- Input groups with icon -->
                  <div class="form-group row">
                      <label for="updateNamaPelanggan" class="col-md-2 col-form-label form-control-label">Nama</label>
                      <div class="col-md-10">
                      <input type="hidden" name="id"  value="{{ $pelanggan->id }}">
                        <input class="form-control" type="nama"  value="{{ $pelanggan->nama_pelanggan }}" name="updateNamaPelanggan" required >
                      </div>
                    </div>
                    <div class="form-group row">
                      <label for="updateAlamat" class="col-md-2 col-form-label form-control-label">Alamat</label>
                      <div class="col-md-10">
                        <input class="form-control" type="alamat" value="{{ $pelanggan->alamat }}"  name="updateAlamat" required>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label for="updateNoTelp" class="col-md-2 col-form-label form-control-label">No.Telp</label>
                      <div class="col-md-10">
                        <input class="form-control" type="notelp" value="{{ $pelanggan->no_telp }}"  name="updateNoTelp" required>
                      </div>
                    </div>
                    <div class="form-group row">
                      <div class="col-md-6">
                        <div class="form-group">
                          <label class="form-control-label" for="updatePoin">POIN</label>
                          <input type="text" class="form-control" value="{{ $pelanggan->poin }}" name="updatePoin">
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group">
                          <label class="form-control-label" for="status_member">Kategori</label>
                         <!-- <h3>{{ $pelanggan->status_member }}</h3>-->
                          <select class="form-control" name="status_member" required="required">
                            <option value="silver" {{ $pelanggan->status_member === 'Silver' ? 'selected' : '' }} >Silver</option>
                            <option value="gold" {{ $pelanggan->status_member === 'Gold' ? 'selected' : '' }} >Gold</option>
                            <option value="diamond" {{ $pelanggan->status_member === 'Diamond' ? 'selected' : '' }} >Diamond</option> 
                          </select>
                        </div>
                      </div>
                    </div>   
            </div>
            <div class="modal-footer">
              <button type="reset" class="btn btn-secondary" data-dismiss="modal">Reset</button>
              <button type="submit" class="btn btn-primary">Update Data</button>
            </div>
            </form>
         </div>
       </div>
    </div>
    @endforeach

this is the route

Route::put('daftar_pelanggan/update/{id}', '\App\Http\Controllers\DaftarPelangganController@update')->name('daftar_pelanggan.update');

and this is the controller

 public function update(Request $request, $id)
    {
               
            $update_pelanggan = DaftarPelanggan::find($id);
            $update_pelanggan->nama_pelanggan = $request->updateNamaPelanggan;
            $update_pelanggan->alamat = $request->updateAlamat;
            $update_pelanggan->no_telp = $request->updateNoTelp;
            $update_pelanggan->poin = $request->updatePoin;
            $update_pelanggan->status_member = $request->updateKategori;
            $update_pelanggan->save();
            

            if($simpan){
                Alert::success(' Berhasil Tambah data ', ' Silahkan dicek kembali');
            }else{
                Alert::error('data gagal disimpan ', ' Silahkan coba lagi');
            }
            return redirect()->back();}

what's wrong with this code ?

1

1 Answers

0
votes

You have named the select control as status_member in the form while you are using $request->updateKategori in controller method/action while saving update data.

You can cross check by dd($request->all()) as first line of the update method and check which key value pairs are available as request data.

Change the update method like

public function update(Request $request, $id)
 {
               
    $update_pelanggan = DaftarPelanggan::findOrFail($id);
    $update_pelanggan->nama_pelanggan = $request->updateNamaPelanggan;
    $update_pelanggan->alamat = $request->updateAlamat;
    $update_pelanggan->no_telp = $request->updateNoTelp;
    $update_pelanggan->poin = $request->updatePoin;

    //name of key $request->status_member must match name=status_member on select control in the form
    $update_pelanggan->status_member = $request->status_member; 
    $update_pelanggan->save();

    //------------- OR you can simply do ---------------------------//

    //$update_pelanggam = DaftarPelanggan::findOrFail($id);

    //$update_pelaggan->update($request->all();

    //-------------------------------------------------------------//
            

    if($simpan){
        Alert::success(' Berhasil Tambah data ', ' Silahkan dicek kembali');
    }else{
        Alert::error('data gagal disimpan ', ' Silahkan coba lagi');
    }

    return redirect()->back();
}