Departemen =
‘nama_departemen’,
‘nama_manager’,
‘jumlah_pegawai’,
Pegawai = ‘nomor_induk_pegawai’,
‘nama_pegawai’,
‘id_departemen’,
’email’,
‘telepon’,
‘gender’,
‘status’
PegawaiController
public function index()
{
$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);
return view ('pegawai.index', compact('pegawai'));
}
public function destroy($id)
{
Pegawai::find($id)->delete();
return redirect()->route('pegawai.index')->with(['success'=> 'Item Berhasil Dihapus']);
}
public function edit($id)
{
$pegawai=Pegawai::join('departemens','pegawais.id_departemen','=','departemens.id')->find($id);
$departemen = Departemen::all();
return view('pegawai.edit',compact('pegawai','departemen'));
}
pegawai.index
@forelse ($pegawai as $item)
<tr>
<td class="text-center">{{ $item->nomor_induk_pegawai }}</td>
<td class="text-center">{{ $item->nama_pegawai }}</td>
<td class="text-center">{{ $item->nama_departemen }}</td>
<td class="text-center">{{ $item->email }}</td>
<td class="text-center">{{ $item->telepon }}</td>
@if($item->gender==0)
<td>Wanita</td>
@else
<td>Pria</td>
@endif
@if($item->status==0)
<td>Inactive</td>
@else
<td>Active</td>
@endif
<td class="text-center">
<form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('pegawai.destroy', $item->id ) }}" method="POST">
<a href="{{ route('pegawai.edit', $item->id) }}" class="btn btn-sm btn-primary">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Hapus</button>
</form>
</td>
</tr>
@empty
<div class="alert alert-danger">
Data Pegawai belum tersedia
</div>
@endforelse
routes.web
Route::get('/', function () {
return view('dashboard');
});
Route::resource('/departemen',AppHttpControllersDepartemenController::class);
Route::resource('/pegawai',AppHttpControllersPegawaiController::class);
so i tried the Hapus and Edit button. it returning id_departemen instead id from table pegawai. i assume this is because im using join. i use join because i want to show nama_departemen from table departemen in pegawai. so what should i do to fix this.
2
Answers
The issue here is that your are joining two tables that both have an ID column.
To get around this you need to be specific in what columns you want to return.
For example;
Hope that helps.
maybe table departemen should look like this
and table pegawai should look like this :
and the controller should look like this :
just choose which do you want to use, id_pegawai or id_departemen
you can change the join with leftjoin if you need
read this documention for another resolved