我有
- 带有字段“id”、“transaction_id”、“date”、“amount”、“payment_method_id”、“additional_note”的付款数据表
- 带有字段 'id'、'name'、'code' 的 payment_methods 表
我想要实现的是在支付数据表中显示 payment_method->name 而不是 payment_method_id
这是我的数据表代码
<?php
namespace App\DataTables;
use App\Models\Payment;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;
class PaymentDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
$dataTable = new EloquentDataTable($query);
return $dataTable->addColumn('action', 'payments.datatables_actions');
}
/**
* Get query source of dataTable.
*
* @param \App\Models\Post $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(Payment $model)
{
return $model->newQuery();
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
->addAction(['width' => '120px'])
->parameters([
'dom' => 'Bfrtip',
'order' => [[0, 'desc']],
'buttons' => [
['extend' => 'create', 'className' => 'btn btn-default btn-sm no-corner',],
['extend' => 'export', 'className' => 'btn btn-default btn-sm no-corner',],
['extend' => 'print', 'className' => 'btn btn-default btn-sm no-corner',],
['extend' => 'reset', 'className' => 'btn btn-default btn-sm no-corner',],
['extend' => 'reload', 'className' => 'btn btn-default btn-sm no-corner',],
],
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
'transaction_id',
'date',
'amount',
'payment_method_id',
'additional_note'
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'paymentsdatatable_' . time();
}
}
我四处寻找,知道它必须在
public function query(Payment $model)
{
return $model->newQuery();
}
但还没有找到有效的解决方案..也许有什么建议
注意:我也在这里发布过询问https://github.com/InfyOmLabs/laravel-generator/issues/582的帖子,但还没有回复