Home > other >  Returning wrong id in laravel
Returning wrong id in laravel

Time:10-10

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 >{{ $item->nomor_induk_pegawai }}</td>
                                            <td >{{ $item->nama_pegawai }}</td>
                                            <td >{{ $item->nama_departemen }}</td>
                                            <td >{{ $item->email }}</td>
                                            <td >{{ $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 >
                                                <form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('pegawai.destroy', $item->id ) }}" method="POST">
                                                    <a href="{{ route('pegawai.edit', $item->id) }}" >Edit</a>
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" >Hapus</button>
                                                </form>
                                            </td>
                                        </tr>
                                    @empty
                                    <div >
                                        Data Pegawai belum tersedia
                                    </div>
                                    @endforelse

routes.web

Route::get('/', function () {
    return view('dashboard');
});
Route::resource('/departemen',\App\Http\Controllers\DepartemenController::class);
Route::resource('/pegawai',\App\Http\Controllers\PegawaiController::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.

CodePudding user response:

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;

$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->select('pegawais.id','departemens.otherColumnName')->paginate(5);

Hope that helps.

CodePudding user response:

maybe table departemen should look like this

'id', 'nama_departemen', 'nama_manager', 'jumlah_pegawai'

and table pegawai should look like this :

'id','nomor_induk_pegawai', 'nama_pegawai', 'id_departemen', 'email', 'telepon', 'gender', 'status'

and the controller should look like this :

Pegawai::select ('pegawai.nomor_induk_pegawai','pegawai.nama_pegawai','departemen.nama_departemen','pegawai.email','pegawai.telepon','pegawai.gender','pegawai.status','pegawai.id as id_pegawai','departemen.id as id_departemen')->Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);

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

https://laravel.com/docs/9.x/queries#select-statements
  • Related