Как добавить столбец в DataTable и вернуть значение из базы данных, используя Laravel - PullRequest
1 голос
/ 30 января 2020

Я пытаюсь получить новый столбец «Вариант использования», используя DataTables, но когда я пытаюсь получить значение tc_scenario из базы данных и вернуть это значение в Html, используя $data->tc_scenario, я получил ошибку, подобную этой: 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, и это моя таблица в база данных

enter image description here

1 Ответ

0 голосов
/ 30 января 2020

В вашей переменной данных нет данных. Попробуйте этот запрос.

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

и затем верните переменную $data.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...