我根据id删除了table中的一条数据,但删除的是另一个id,其值较小

I deleted one of the data in the table based on id, but what was deleted was another id whose value is smaller

我从 2 周前开始学习 Laravel 框架。

在这里,我有 2 个 tables,即 anggotas 和 simpanans 具有一对多关系, 一个 anggota (id_anggota as pk) 可以有很多 simpanan(id simpanan as pk)。 我在 simpanans table 中使用 id_anggota 作为外键,但是 当我根据 id 删除 simpanans table 中的一行时,它会删除另一个值较小的 id。

这是Simpanan模型中的模型

    <?php

    namespace App\Models;
    
    use Carbon\Carbon;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Support\Facades\DB;

   class Simpanan extends Model
   

   {
    use HasFactory;

    // protected $table='simpanans';
    protected $primaryKey = 'id_simpanan';
    protected $keyType = 'string';
    
    protected $fillable = [
        'no_anggota','tgl','tabungan','s_wajib','s_thr','s_pendidikan','lain','catatan'
    ];

    public function anggota(){
        return $this->belongsTo(Anggota::class);
    }
    
}

这是Anggota模型中的模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Anggota extends Model
{
    use HasFactory;

    protected $primaryKey = 'id_anggota';
    protected $keyType = 'string';

    protected $fillable = [
        'id_anggota','name','tmlahir','tglahir','alamat','ktp','pendidikan','pekerjaan','hp',
    ];

    public function detailAnggota($id){
        return DB::table('anggotas')->where('id_anggota', $id)->first();
    }

    public function simpanan(){
        return $this->hasMany(Simpanan::class,'no_anggota','id_anggota');
    }

}

这是控制器

<?php

namespace App\Http\Controllers;

use App\Models\Simpanan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\View\Concerns\ManagesLayouts;

class SimpananController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('layouts.simpanan.simpanan');
    }
    
    public function __construct()
    {
        $this->Simpanan = new Simpanan();
    }

    public function simpan(){
        $data=[
            'simpanan'=>$this->Simpanan->joinAnggota()
        ];
            return view('layouts.simpanan.simpanan',$data);
    }

    public function store(Request $request)
    {
        $rules=[
            'id_anggota'=>'required',
            'tglsetor'=>'required',
            'tabungan'=>'required',
            's_wajib'=>'required',
            's_thr'=>'required',
            's_pendidikan'=>'required',
            'lain'=>'required',
            'catatan'=>'nullable'
        ];
        $messages = [
            'tglsetor.required'=> 'Tanggal harus diisi!!!',
            'tabungan.required'=> 'Tabungan harus diisi dengan angka, atau diisi dengan angka 0 !!!',
            's_wajib.required'=> 'Simpanan Wajib harus diisi dengan angka, atau diisi dengan angka 0 !!!',
            's_thr.required'=> 'Simpanan THR harus diisi dengan angka, atau diisi dengan angka 0 !!!',
            's_pendidikan.required'=> 'Simpanan Pendidikan harus diisi dengan angka, atau diisi dengan angka 0 !!!',
            'lain.required'=> 'Dana Lain harus diisi dengan angka, atau diisi dengan angka 0 !!!',
        ];
 
        $validator = Validator::make($request->all(), $rules, $messages);
         
        if($validator->fails()){
            return redirect()->back()->withErrors($validator)->withInput($request->all());
        }

        $simpan = new Simpanan();
        $simpan->anggota_id_anggota = $request->id_anggota;
        $simpan->tgl = $request->tglsetor;
        $simpan->tabungan = $request->tabungan;
        $simpan->s_wajib = $request->s_wajib;
        $simpan->s_thr = $request->s_thr;
        $simpan->s_pendidikan = $request->s_pendidikan;
        $simpan->lain = $request->lain;
        $simpan->catatan = $request->catatan;
        $simpan->save();
        return redirect('/simpanan')->with('status','Data berhasil ditambahkan');

    }

    public function edit($id)
    {
        $simpanan=Simpanan::find($id);
        return view('layouts.simpanan.edit',['simpanan'=>$simpanan]);
    }

    public function update(Request $request, $id)
    {
        
        $simpanan=Simpanan::find($id);
        $simpanan->update([
            'tgl'=>$request->tglsetor,
            'tabungan'=>$request->tabungan,
            's_wajib'=>$request->s_wajib,
            's_thr'=>$request->s_thr,
            's_pendidikan'=>$request->s_pendidikan,
            'lain'=>$request->lain,
            'catatan'=>$request->catatan
        ]);
        return redirect('/simpanan')->with('status','Data Berhasil Dirubah');
        
    }

    public function destroy($id)
    {
        $simpanan=Simpanan::findOrFail($id)->where('id_simpanan',$id);
        $simpanan->delete();
        return redirect('/simpanan')->with('status','Data Berhasil Dihapus');
    }

}

这是视图

<section class="content">
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-header">
                  <h3 class="card-title">History Simpanan</h3>
                </div>
                <!-- /.card-header -->
                <div class="card-body">
                  <table class="table table-bordered table-striped">
                    <thead class="thead-dark">
                      <tr>
                        <th scope="col">No.</th>
                        <th scope="col">ID Transaksi</th>
                        <th scope="col">Tanggal</th>
                        <th scope="col">Nomor Anggota</th>
                        <th scope="col">Nama</th>
                        <th scope="col">Tabungan</th>
                        <th scope="col">S. Wajib</th>
                        <th scope="col">S. THR</th>
                        <th scope="col">S. Pendidikan</th>
                        <th scope="col">Aksi</th>
                      </tr>
                    </thead>
                    @php $no=1; @endphp
                    @foreach ($simpanan as $data)
                    <tbody>
                      <tr>
                        <td>{{$no++}}</td>
                        <td>STR{{$data->id_simpanan}}</td>
                        <td>{{$data->tgl}}</td>
                        <td>{{$data->id_anggota}}</td>
                        <td>{{$data->name}}</td>
                        <td>{{$data->tabungan}}</td>
                        <td>{{$data->s_wajib}}</td>
                        <td>{{$data->s_thr}}</td>
                        <td><span class="badge bg-danger">{{$data->s_pendidikan}}</span></td>
                        <td>
                          <div class="text-right">
                            <a href="/simpanan/edit/{{$data->id_simpanan}}" class="btn btn-sm btn-success">
                              <i class="fas fa-user"></i> Edit
                            </a>
                            <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#modal-sm">
                              <i class="fas fa-user"></i> Hapus
                            </button>
                          </div>
                      </tr>
                    </tbody>
                    @endforeach
                  </table>
                </div>
                <!-- /.card-body -->
                <div class="modal fade" id="modal-sm">
                  <div class="modal-dialog modal-sm">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h4 class="modal-title">Yakin hapus data?</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <p>Data yang dihapus tidak dapat kembali&hellip;</p>
                      </div>
                      <div class="modal-footer justify-content-between">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
                        <a href="/simpanan/hapus/{{$data->id_simpanan}}" methode="post" class="btn btn-sm btn-danger">
                          <i class="fas fa-user"></i> Hapus
                        </a>
                      </div>
                    </div>
                    <!-- /.modal-content -->
                  </div>
                  <!-- /.modal-dialog -->
                </div>
                <!-- /.modal -->
                <div class="d-flex justify-content-center">{{$simpanan->links()}}</div>
              </div>
              <!-- /.card -->
            <!-- /.col -->
            </div>
          <!-- /.row -->
        </div><!-- /.container-fluid -->
      </section>
      <!-- /.content -->
 try this 
$simpanan=Simpanan::where('id_simpanan',$id);
        $simpanan->delete();

以上代码将删除所有具有给定 id 的 simpanan。

$simpanan=Simpanan::find($id)->delete();

以上代码将删除与给定 id 匹配的一行。

在 table 中,您可以添加一个事件侦听器来侦听像这样单击按钮以调用获取 ID 的 JavaScript 函数:

<td>
  <div class="text-right">
      <a
        href="/simpanan/edit/{{$data->id_simpanan}}"
        class="btn btn-sm btn-success"
      >
        <i class="fas fa-user"></i> Edit
      </a>
      <button
        type="button"
        class="btn btn-sm btn-danger"
        data-toggle="modal"
        data-target="#modal-sm"
        onclick="pass_id_to_modal({{ $data->id }})"
      >
        <i class="fas fa-user"></i> Hapus
      </button>
    </div>
</td>

此外,对于模式,我们可以为 href 设置“#”,因为我们将动态设置它,而不是我们可以为按钮添加一个 ID,以便通过 JavaScript 函数访问,就像这样:

<!-- modal -->
<div class="modal fade" id="modal-sm">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Yakin hapus data?</h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <p>Data yang dihapus tidak dapat kembali&hellip;</p>
        </div>
        <div class="modal-footer justify-content-between">
          <button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
          <a id="modal_delete_link" href="#" methode="post" class="btn btn-sm btn-danger">
            <i class="fas fa-user"></i> Hapus
          </a>
        </div>
      </div>
    </div>
</div>

现在,您可以创建一个 JS 文件或在视图本身中添加以下函数来处理模态的 ID 传输:

function pass_id_to_modal(id) {
  var delete_button = document.getElementById("modal_delete_link");
  delete_button.href = "/simpanan/hapus/" + id;
}