Laravel datatable. Illegal string offset 'start'

时间:2018-04-18 17:51:20

标签: php laravel datatable onclick

I have a datatable that works great. However, on the last column I have a button which should do another query and then build a second datatable, based on these results.

As you can see on the following example, when I click on the button 'searchMom' in the first row, it should run a query based on john newman´s mother('mary newman') and put the results on the panel called "Mom".

enter image description here





But it´s not working.
Please see the image and the code below: enter image description here





View: viewFamily.blade.php

<div class="row">
    <div class="col-md-12" id="rowFamily01">
        <div class="panel">
            <div class="panel-heading">
                <div class="box-tools pull-right">
                    <button type="button" class="btn btn-xs panel_expand"><i class="material-icons md-18">expand_less</i></button>
                </div>
                <div class="panel-title">Find a person</div>
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-9">
                        <div class="form-group">
                            <label class="control-label" for="inputPersonSearch" >Search: </label>
                            <input id="inputPersonSearch" class="form-control" type="text" name="inputPersonSearch" form="formPerson">
                        </div>
                    </div>
                     <div class="col-md-3">
                        <div class="form-group">
                            <label class="control-label" for="btnPersonSearch" style="color: white;">Search:</label>
                            <button id="btnPersonSearch" class="form-control btn btn-success" type="submit" title="Search" form="formPerson">Search</button>
                        </div>
                    </div>
                </div>
                <div class="row" style="width:100%; margin-left: 0%;">
                     <div class="col-md-12 datatable" id="datatable-main"> 
                     </div>
                </div>
            </div>
        </div>

    </div>
</div>

routes: diego_routes.php

 Route::group(['prefix' => 'diego', 'namespace' => 'Diego'], function () {
     Route::get("/", "DiegoController@index")->name("index");
     Route::get("viewFamily", "DiegoController@viewFamily")->name("viewFamily");
     Route::post("searchPerson", "DiegoController@searchPerson")->name("searchPerson");
     Route::get("searchMom/{mom}", "DiegoController@searchMom")->name("searchMom");   
});

controller: DiegoController.php

public function searchMom($mom){

   $input = $mom;
   //dd($input);


    //begining select query
    $query = TD_CP_CIDADAO::where('CID_INT_ID_CIDADAO', '>=', "0");
    if(!empty($input["filtro"])){
        foreach($input["filtro"] as $k=>$v){
            if(!empty($k) and !empty($v)){
                //$searchValues = preg_split('/,/', $v, -1, PREG_SPLIT_NO_EMPTY);    
                $searchValues = array_map('trim', $v);
                //dd($searchValues);
                $query->where(function ($q) use ($searchValues) {
                  foreach ($searchValues as $value) {
                    $q->orWhere('CID_STR_DS_NOME', 'like', "%{$value}%");

                  }
                });  
            }
        }
    }

    //order data
    if(!empty($input["order"]) and count($input["order"]) > 0)
    {
        foreach($input["order"] as $v){
            $position = $v["column"];
            $column = $input["columns"][$position]["data"];
            $dir = !empty($v["dir"])? $v["dir"] : "asc";
            $query->orderBy($column, $dir);
        }
    }

    //other params...
    $query->skip($input["start"])->take($input["length"]);
    $json_data["data"] = $query->get()->toArray();
    $json_data["recordsTotal"] = $query->count();
    $json_data["recordsFiltered"] = $json_data["recordsTotal"];
    $json_data["draw"] = $input["draw"];


    return response()->json($json_data);    

}

javascript: diego.js

$(document).on("click", ".btnMom", function(e){
    e.preventDefault();

    $("#newPanels").empty();
    $("#newPanels").append(panelMom);
    $("#datatable-mom").append(tableMom);
    $("#datatable-mom").css("font-size", "12px");

    if($.fn.dataTable.isDataTable('table#tbMom')){
       $("table#tbMom").DataTable().destroy();
    }

    $.fn.dataTable.ext.errMode = 'throw';

    var filtros = $(this).serializeArray().reduce(function(a, x) {    a[x.name] = x.value;    return a;  }, {});


    var options = {
        serverSide: true,
        processing: true,
        retrieve: true,
        ajax: {
          dataSrc: 'data',
          url : $(this).data("url"),
          type: 'get',
          async: false,
          data: {filtro:filtros}
        },
        columns : columnsMom,
        lengthMenu : [ 5, 10, 25 ]
     }; 

    $("table#tbMom").DataTable(options);
    });


var columnsMom = [
    { data: 'CID_INT_ID_CIDADAO', visible:false},
    { data: 'CID_STR_DS_NOME'},
    { data: 'CID_DAT_DT_NASCIMENTO',
        render: function(data, type, row){
                  return converterData(data); 
        }, className: "text-center"},
    { data: 'CID_STR_NM_MAE'},
    { data: 'CID_STR_NM_PAI'},
    { data: 'CID_INT_NR_RG', className: "text-center"},
    { data: 'CID_INT_NR_CPF', className: "text-center"},
    { data: 'acao',
        render: function(data, type, row, meta){

               var b0 = '\n\
                        \n\
                        ';

               return b0;
        },orderable: false, className: "text-center" 
}]; 


var panelMom = 
    `
    <div class="panel" id="panelMom">
        <div class="panel-heading">
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-xs panel_expand"><i class="material-icons md-18">expand_less</i></button>
                <button type="button" class="btn btn-xs panel_remove"><i class="material-icons md-18">close</i></button>
            </div>
            <div class="panel-title">Mom</div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 datatable conteudod" id="datatable-mom"> 
                </div>
            </div>
        </div>
    </div>
`;


var tableMom = 
    ` 
    <table id="tbMom" class="table table-hover compact" cellspacing="0" width="100%" >
        <thead>
           <tr>  
                <th class="hidden">ID</th>
                <th>NAME</th>
                <th>DOB</th>
                <th>MOM</th>
                <th>DAD</th>
                <th class="text-center">DOC #</th>
                <th class="text-center">FIELD #</th>
                <th class="text-center">ACTION</th>
           </tr>
       </thead>
       <tbody>
       </tbody>
       <tfoot>
       </tfoot>
    </table> 
`;



The error message: enter image description here

The line 219 (controller) shown in error message: enter image description here

Results on "dd($input)" inside the controller: enter image description here

I appreciate any help to solve this problem! Thank you!

1 个答案:

答案 0 :(得分:0)

You're setting the value of $input to the value of $mom, which is the last value in your URL, so mom equals Mary Newman but the rest of your code expects $input to be an array of parameters like order, columns and start.

You should assign the value of $input as the request parameters, e.g:

public function searchMom($mom)
{
    $input = request()->input();
}

Alternatively instead of $input being an array you could use the Request object directly, e.g:

public function searchMom(Request $request, $mom)
{
    $request->order;
    $request->columns;
    $request->start;
}