我未能保存更新数据,通知错误消息为SQLSTATE [23000]:违反完整性约束:1048列'status_member'不能为空(SQL:update daftar_pelanggans set alamat = gdsgzddgrrse,status_member =?,daftar_pelanggansupdated_at = 2020- 12-06 03:36:35其中id = 6)
这是按钮编辑

 <button data-toggle="modal" data-target="#editModal-{{ $pelanggan->id }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></button>
这是模态 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
这是路线
Route::put('daftar_pelanggan/update/{id}', '\App\Http\Controllers\DaftarPelangganController@update')->name('daftar_pelanggan.update');
这是 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();}
此代码有什么问题?

最佳答案

在保存更新数据的同时,在 Controller 方法/操作中使用$ request-> updateKategori时,已在表单中将选择控件命名为status_member
您可以通过dd($request->all())作为更新方法的第一行进行交叉检查,并检查哪些键值对可用作请求数据。
像这样更改更新方法

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();
}

关于sql - SQLSTATE [23000] : Integrity constraint violation: 1048 Column 'status_member' cannot be null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/65164468/

10-09 01:48