我试图使用DataTables获取新列“用例”,但是当我尝试从数据库中获取值tc_scenario并使用$data->tc_scenario将此值返回给Html时,出现了如下错误:
Undefined variable: data

public function dtindex(Request $request)
    {
        $tanggal = $request->input('tanggal');
        $selectorx= $request->input('selector1');
        $selectory= $request->input('selector2');
        $selectorz= $request->input('selector3');
        $data = DB::select(DB::raw("
        select DISTINCT id, tc_scenario from tbl_master
        "));
        $data= collect($data);
        return Datatables::of($data)
            ->addColumn('tc_scenario', function ($data) use ($selectorx, $selectory, $selectorz){
                $selector1 = $selectorx;
                $selector2 = $selectory;
                $selector3 = $selectorz;
                return value($data, compact('selector1', 'selector2', 'selector3'));
            })
            ->make(true);
    }


脚本:

$(function () {
        $('#dtindex').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: '{!! route('dtindex') !!}',
                type: 'POST',
                data: function (d) {
                    d.selector1 = '{{$selector1}}';
                    d.selector2 = '{{$selector2}}';
                    d.selector3 = '{{$selector3}}';
                    d._token = $('meta[name="csrf-token"]').attr('content');
                }
            },
            columns: [
                {
                    render: function (data, type, row, meta) {
                        return meta.row + meta.settings._iDisplayStart + 1;
                    }
                },
                {
                    data: 'tc_scenario', name: 'tc_scenario',
                    render: function (data, type, row, meta) {
                        return '<form name="detailcust" method="post" action="{!! route('report') !!}">',
                        '<input name="_token" type="hidden" value="csrf_token()"/>',
                        '<input name="inputan" id="inputan" type="hidden" value="{{$selector1}}"/>' ,
                        '<input name="inputan" id="inputan" type="hidden" value="{{$selector2}}"/>',
                        '<input name="inputan" id="inputan" type="hidden" value="{{$selector3}}"/>',
                        '<input class="btn btn-link btn-xs" type="submit" name="submit" value="{{$data->tc_scenario}}" id="submit"><h4 style="visibility:hidden">{{$data->tc_scenario}}</h4></input></form>';
                    }
                }
            ]
        });
    });


HTML:

<div class="row">
                            <div class="col-md-pull-12 table-responsive">
                                <table class="table table-bordered table-striped"
                                       style="font-size: 10px; text-align: center;"
                                       id="dtindex">
                                    <thead>
                                    <tr>
                                        <th style="text-align: center">No.</th>
                                        <th style="text-align: center">Use Case</th>
                                    </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>


我希望能够从tc_scenario获取价值,这是我在数据库中的表

javascript - 如何使用Laravel在DataTable中添加列并从数据库返回值-LMLPHP

最佳答案

您的数据变量中没有数据。试试这个查询。

$data = DB::table('tbl_master')->select('id','tc_scenario')->distinct('tc_scenario')->get().


然后返回$data变量。

09-21 00:02