if (request()->ajax()) {
$query = BangunanKategori::with('bidang');
return DataTables::eloquent($query)
->addColumn('nama_bidang', function ($item) {
return $item->bidang->nama;
})
->addColumn('kode_bidang', function ($item) {
return $item->bidang->kode;
})
->toJson();
}
I have a problem displaying the DataTable code above because the relation table has the same column names. If the data is sorted based on kode_bidang and nama_bidang the data will be duplicated from the bidang relation table.
Please help me fix this. I have tried various methods, but the data displayed remains duplicate if ordered via these two columns.
You can use Laravel's
groupByandselectmethods to ensure that only distinctbidangrecords are retrieved from the database.