我需要向Yajra数据表添加具有(BelongsTo)关系的列。
我添加了一个新列,但搜索无效。
如何使用HTML BUILDER添加具有关系的列?
这是数据库中的“证书”表(截图)
任何帮助将不胜感激。最好的祝福。
我在Datatables/CertificatesDataTable.php
中的代码
namespace App\DataTables;
use App\Certificate;
use Yajra\DataTables\Services\DataTable;
class CertificatesDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
return datatables($query)
->addColumn('worker_name', function ($certificate) {
$certificate = Certificate::findOrFail($certificate->id);
return $certificate->worker->name;
})
->setRowClass(function ($certificate) {
if ($certificate->date_next < date('Y-m-d')) {
return 'barred';
} elseif ($certificate->date_next <= date('Y-m-d', strtotime('+ 10 days'))) {
return 'expired';
}
})
;
}
/**
* Get query source of dataTable.
*
* @param \App\Certificate $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(Certificate $model)
{
return $model->newQuery()->select
(
'id',
'number',
'date_create',
'date_next'
);
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns([
['data' => 'id', 'title' => 'ID', 'width' => '5%'],
['data' => 'number', 'title' => 'Number certificate'],
['data' => 'worker_name', 'title' => 'Worker name'],
['data' => 'date_create', 'title' => 'Date create'],
['data' => 'date_next', 'title' => 'Date next']
])
->parameters([
'dom' => 'lftBipr',
'order' => [ [0, 'desc'] ],
'buttons' => [
[
'extend' => 'collection',
'text' => '<i class="fa fa-print"></i> Export/Print',
'buttons' => [
'excel',
'print'
]
]
],
'language' => [
'url' => url('//cdn.datatables.net/plug-ins/1.10.16/i18n/English.json')
]
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
'id',
'number',
'date_create',
'date_next'
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'Certificate_' . date('Y-m-d/H-i');
}
}
我在模板中使用{!! $dataTable->table() !!}
和{!! $dataTable->scripts() !!}
。