I have started learning Laravel framework since 2 weeks ago.
Here, I have 2 tables, namely anggotas and simpanans which has one-to-many relationship, one anggota (id_anggota as pk) can have many simpanan(id simpanan as pk). I have used id_anggota as a foreign key in the simpanans table, but when I delete one of the rows in the simpanans table based on id, it deletes another id whose value is smaller.
this is the models in Simpanan model
<?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);
}
}
this is the models in Anggota model
<?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');
}
}
this the controller
<?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');
}
}
and this the view
<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">×</span>
</button>
</div>
<div class="modal-body">
<p>Data yang dihapus tidak dapat kembali…</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 -->
In the table you can add an event listener to listen for click on the button like this to call a JavaScript function that gets the ID:
<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>
Also, for the modal, we can set "#" for href because we are going to set it dynamically, instead we can add a ID for the button to be accessible via the JavaScript function like this:
<!-- 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">×</span>
</button>
</div>
<div class="modal-body">
<p>Data yang dihapus tidak dapat kembali…</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>
Now, you can either create a JS file or add the following function inside the View itself to handle the ID transfer the modal:
function pass_id_to_modal(id) {
var delete_button = document.getElementById("modal_delete_link");
delete_button.href = "/simpanan/hapus/" + id;
}