1

I am trying to get a new column "Use Case" using DataTables, but when I trying to get value tc_scenario from database and return this value to Html using $data->tc_scenario I got an error like this : 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);
    }

Scripts :

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

I expect to be able to get value from tc_scenario, and this is my table in database

enter image description here

elkecp
  • 21
  • 1
  • 1
  • 4
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Aksen P Jan 30 '20 at 07:16

1 Answers1

0

There is no data in your data variable. try this query.

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

and then return the $data variable.

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
Umer
  • 86
  • 5
  • This is my function : `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);` I got an error : **Undefined variable: data** – elkecp Jan 30 '20 at 07:58