0

我有

  • 带有字段“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的帖子,但还没有回复

4

2 回答 2

3

使用 InfyOm Laravel 生成器将关系数据显示到 Yajra 数据表中

需要修改数据表文件的getColumn()方法。

protected function getColumns()
{
    return [
        'title',
        'author_name' => new \Yajra\DataTables\Html\Column(['title' => 'Author Name', 'data' => 'author.name', 'name' => 'author.name'])
    ];
} 

和修改查询方法返回关系数据

public function query(Post $model)
{
    return $model->newQuery()->with(['author']);
}

这里有完整的博客示例:https ://blog.infyom.com/how-to-display-relationship-data-into-yajra-datatables-with-infyom-laravel-generator

于 2019-11-26T06:25:31.840 回答
1

有一段时间没有使用 Yajra 表了,但我做了这样的事情。根据您命名 和 之间关系的方式PaymentPaymentMethod修改它以适合您的情况。

public function query(Payment $model)
{
    return $model->newQuery()->with(['paymentMethod']);

    // specify columns if you like
    // return $model->newQuery()->with(['paymentMethod'])->select('payments.*');
}

...

protected function getColumns()
{
    return [
        'transaction_id',
        'date',
        'amount',
        [
            'name' => 'paymentMethod.name',
            'data' => 'paymentMethod.name',
            'title' => 'Payment Method',
            'defaultContent' => '',
        ],
        'additional_note',
    ];
}
于 2018-11-28T03:48:11.053 回答