在从 infyom 生成的 yajra 数据 table 中显示 field_name 来自加入 table 而不是 field_id

show field_name from joined table instead of field_id in yajra datatables that generated from infyom

我有

我想要实现的是支付数据table 它显示 payment_method->name 而不是 payment_method_id

这是我的数据table代码

 <?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();
        }

但还没有找到可行的解决方案..也许有任何建议

nb:我 post 也在这里 https://github.com/InfyOmLabs/laravel-generator/issues/582 询问过,但还没有回复

有一段时间没有使用 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',
    ];
}

使用 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'])
    ];
} 

并将查询方法修改为return关系数据

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