i have projext with laravel and datatable using yajrabox, it stuck in relationship sort
<?php
namespace App\DataTables;
use DB;
use App\Models\Kabupaten;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;
class KabupatenDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
return datatables()
->eloquent($query)
->addIndexColumn()
->addColumn('kordinat', function(Kabupaten $kabupaten) {
return $kabupaten->koordinat_bujur.",".$kabupaten->koordinat_lintang;
})
// ->addColumn( 'action', function($data){ // $data is data od current row in table
// return view( 'kabupaten.action', [ 'data' => $data ] );
// } );
->addColumn('action', 'kabupaten.action');
}
/**
* Get query source of dataTable.
*
* @param \App\Models\Kabupaten $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(Kabupaten $model)
{
// return $model->newQuery()->with('provinsi');
return Kabupaten::with('provinsi')->select('kabupaten.*','provinsi.provinsi');
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->setTableId('kabupaten-table')
->columns($this->getColumns())
->minifiedAjax()
->dom('Bfrtip')
->orderBy(1)
->parameters([
'buttons' => ['excel'],
])
->buttons(
Button::make('create'),
Button::make('export'),
Button::make('print'),
Button::make('reset'),
Button::make('reload')
);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
Column::make('DT_RowIndex')->title('No')->orderable(false)->searchable(false),
Column::make('provinsi.provinsi'),
Column::make('status'),
Column::make('kabupaten')->title('Nama kabupaten'),
Column::make('ketinggian'),
Column::make('kordinat')
->exportable(false)
->printable(false),
// Column::make('keterangan'),
Column::computed('action')
->exportable(false)
->printable(false),
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'Kabupaten_' . date('YmdHis');
}
}
this is my kabupaten datatable class
<form id="delete_form" method="POST">
@csrf
@method('DELETE')</form>
<a type="button" href="{{route('adm.kabupaten.show', ['id' => $id])}}" class="btn btn-just-icon btn-round btn-sm btn-info" data-toggle="tooltip" data-placement="bottom" title="detail">
<i class="fa fa-eye"></i>
</a>
<a href="{{route('adm.kabupaten.edit', ['id' => $id])}}" hljs-string">" data-toggle="tooltip" data-placement="bottom" title="ubah">
<i hljs-string">"></i>
</a>
<a href="#" data-url="{{ route('adm.kabupaten.destroy', $id) }}" onclick="return deleteConfirm(this);">
<i class="fa fa-trash"></i>
</a>
<script>
$(function() {
$('[data-toggle="tooltip"]').tooltip();
})
</script>
and this is my action column.
provinsi model
public function kabupaten()
{
return $this->hasMany(Kabupaten::class);
}
and kabupaten model
public function provinsi()
{
return $this->belongsTo(Provinsi::class);
}
my question are why $id in kabupaten.action return provinsi.id instead of kabupaten.id
and i have try
return Kabupaten::with('provinsi')->select('kabupaten.*','provinsi.provinsi');
the $id return kabupaten.id but when i try to sort column, it said coloum provinsi.provinsi not found couse the yajra not join my query. if there anything wrong with my code. thanks
CodePudding user response:
remporerary solve
protected function getColumns()
{
return [
Column::make('DT_RowIndex')->title('No')->orderable(false)->searchable(false),
Column::make('provinsi.provinsi')->orderable(false),
Column::make('status'),
Column::make('kabupaten')->title('Nama kabupaten'),
Column::make('ketinggian'),
Column::make('kordinat')
->exportable(false)
->printable(false),
// Column::make('keterangan'),
Column::computed('action')
->exportable(false)
->printable(false),
];
}
im add ->orderable(false), in provinsi.provinsi. dont know why it solve the problem
CodePudding user response:
try changing eloquent 'with' to 'join' in query()
public function query(Kabupaten $model)
{
return Kabupaten::query()
->leftJoin('provinsi', 'provinsi.id', '=', 'kabupaten.provinsi_id')
->addSelect([
'kabupaten.*',
'provinsi.provinsi as provinsi_provinsi'
]);
}
and add column
Column::make('provinsi_provinsi')->name('provinsi_provinsi')->title('provinsi_provinsi'),