如何搜索我在 Laravel Yajra Datatables 中添加的具有关系的列?

问题描述 投票:0回答:3

所以我使用 Yajra Datatables 来制作我的数据表。

我添加了如下所示的附加列,以便它可以显示关系中的用户名。

->addColumn('customer_name', function (Order $order){
                return $order->user->name;
            })

虽然此列出现在数据表中,但我无法搜索它。以下是完整的功能代码:

public function viewDTPending(){
        $store= Auth::user()->store;
        $pending = Order::where('store', $store)->where('status', 'Pending')->orderBy('user_id')->groupBy('order_id', 'status', 'order_branch', 'user_id', 'created_at', 'updated_at');
        return Datatables::of($pending)
            ->addColumn('customer_name', function (Order $order){
                return $order->user->name;
            })
            ->editColumn('created_at', function (Order $order){
                    return $order->created_at->diffForHumans();
                })
            ->make(true);
    }

下面是ajax代码:

<script>
    $(function() {
        $('#table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{{ url('getPendingOrdersDT') }}',
            columns: [
                { data: 'order_id', name: 'order_id' },
                { data: 'user_id', name: 'user_id' },
                { data: 'customer_name', name: 'customer_name' },
                { data: 'status', name: 'status' }
            ]
        });
    });
</script>

我怎样才能让它搜索 customer_name 列?

laravel datatables yajra-datatable
3个回答
1
投票

尝试预加载:

$pending = Order::with('user:id,name')->where('store', $store)->where('status', 'Pending')->orderBy('user_id')->groupBy('order_id', 'status', 'order_branch', 'user_id', 'created_at', 'updated_at');
   return Datatables::of($pending)
     ->addColumn('customer_name', function ($row){
          return $row->user['name'];
   })

0
投票

您必须输入表的名称和列。

示例:名称:'users.id'

我们在同一个系统中搜索ajax就是用这个方法

或简单的代码:

    $objects = Model::query()
        ->leftJoin('curs', 'cur.id', '=', 'model.cur_id')
        ->select([
            'models.id',
            'models.address',
            'models.tag',
            'curs.title_en',
        ]);

    return Datatables::of($objects)
        ->addColumn('actions', function ($model) {
            return 'action'
        })
        ->make(true);

0
投票
 $('#flashreport_status_table').DataTable().destroy()
var table = $('#flashreport_status_table').DataTable({
    processing: true,
    serverSide: true,
    order: [0, 'DESC'],
    searchable: true,
    responsive: true,
    ajax: APP_URL + '/backend/flashreportstatus/allFlashreportStatus',
    columns: [
        {
            data: 'DT_RowIndex',
            name: 'id',
            searchable: false
        },
        {
            data: 'flash_report_no',
            name: 'flash_report_no',

        },
        {
            data: 'dealer_comment',
            name: 'dealer_comment',

        },
        {
            data: 'service_eng_status',
            name: 'service_eng_status',

        },
        {
            data: 'service_eng_comment',
            name: 'service_eng_comment',

        },
        {
            data: 'technical_cell_head_assign',
            name: 'technical_cell_head_assign',

        },
        {
            data: 'technical_cell_representative_eng_status',
            name: 'technical_cell_representative_eng_status',

        },
        {
            data: 'technical_cell_representative_eng_comment',
            name: 'technical_cell_representative_eng_comment',

        },
        {
            data: 'technical_cell_head_status',
            name: 'technical_cell_head_status',

        },
        {
            data: 'technical_cell_head_comment',
            name: 'technical_cell_head_comment',

        },
        {
            data: 'technical_cell_head_reject_count',
            name: 'technical_cell_head_reject_count',

        },
        

    ]
})


$('#flashreport_no, #dealer_comment, #service_eng_status, #service_eng_comment, #technical_cell_head_assign, #technical_cell_representative_status, #technical_cell_representative_comment, #technical_cell_head_status, #technical_cell_head_comment, #technical_cell_head_reject_count').on('keyup', function () {
    table.column($(this).parent().index()).search(this.value).draw();
});
© www.soinside.com 2019 - 2024. All rights reserved.