Laravel yajrabox关系排序

z18hc3ub  于 2023-03-31  发布在  其他
关注(0)|答案(2)|浏览(107)

我有一个项目与laravel和datatable使用yajrabox,它卡在关系排序

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

这是我的kabupaten数据表类

<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>

这是我的行动专栏
普罗文西模型

public function kabupaten()
    {
        return $this->hasMany(Kabupaten::class);
    }

和Kabupaten模型

public function provinsi()
    {
        return $this->belongsTo(Provinsi::class);
    }

我的问题是为什么$id在kabupaten.action返回provinsi.id而不是kabupaten.id
我已经尝试

return  Kabupaten::with('provinsi')->select('kabupaten.*','provinsi.provinsi');

$id返回kabupaten.id,但当我尝试对列进行排序时,它说列provinsi.provinsi未找到,因为yajra未加入我的查询。
如果我的代码有什么问题。
谢谢

wlzqhblo

wlzqhblo1#

尝试在query()中将elongent 'with'更改为'join'

public function query(Kabupaten $model)
{   
    return Kabupaten::query()
        ->leftJoin('provinsi', 'provinsi.id', '=', 'kabupaten.provinsi_id')
        ->addSelect([
            'kabupaten.*',
            'provinsi.provinsi as provinsi_provinsi'
        ]);
}

并添加列

Column::make('provinsi_provinsi')->name('provinsi_provinsi')->title('provinsi_provinsi'),
56lgkhnf

56lgkhnf2#

临时解决方案:

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),
    ];
}

我在provinsi.provinsi中添加了->orderable(false),但我不知道为什么它解决了这个问题。

相关问题