I have a project 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])}}" class="btn btn-just-icon btn-round btn-sm btn-primary" data-toggle="tooltip" data-placement="bottom" title="ubah">
<i class="fa fa-edit"></i>
</a>
<a href="#" data-url="{{ route('adm.kabupaten.destroy', $id) }}" class="btn btn-just-icon btn-round btn-sm btn-danger" 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 column provinsi.provinsi not found because the yajra not join my query.
If there anything wrong with my code.
Thanks
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'),
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.