Yajra \ Datatables \ Engines \ CollectionEngine类的对象无法转换为字符串

时间:2018-11-01 08:47:30

标签: ajax laravel datatables eloquent

我正在将jquery数据表用于服务器端处理,但是作为响应,我收到错误Yajra \ Datatables \ Engines \ CollectionEngine类的对象无法转换为字符串。下面是我的代码。

<div class="table-responsive">
  <table class="table table-bordered table-hover toggle-circle default footable-loaded footable" id="users-table">
    <thead>
      <tr>
         <th>@lang('app.id')</th>
         <th>@lang('app.employee_erp')</th>
         <th>@lang('app.guard_name')</th>
         <th>@lang('app.center_name')</th>
         <th>@lang('app.current_post')</th>
         <th>@lang('app.contract')</th>
         <th>@lang('app.mobile')</th>
         <th>@lang('app.gender')</th>
         <th>@lang('app.action')</th>
      </tr>
    </thead>
  </table>

,下面给出了javascript代码

var table = $('#users-table').dataTable({
            responsive: true,
            processing: true,
            serverSide: true,
            stateSave: true,
            ajax: '{!! route('admin.guards.data') !!}',
            language: {
                "url": "<?php echo __("app.datatable") ?>"
            },
            "fnDrawCallback": function( oSettings ) {
                $("body").tooltip({
                    selector: '[data-toggle="tooltip"]'
                });
            },
            columns: [
                { data: 'id', name: 'id' },
                { data: 'employee_erp', name: 'employee_erp' },
                { data: 'guard_name', name: 'guard_name' },
                { data: 'center_name', name: 'center_name' },
                { data: 'current_post', name: 'current_post'},
                { data: 'contract', name: 'contract' },
                { data: 'mobile', name: 'mobile' },
                { data: 'gender', name: 'gender' },
                { data: 'action', name: 'action', width: '15%' }
            ]
        });

控制器代码如下

public function data() {

    $gurads = GuardDetails::all();

    return Datatables::of($gurads)
        ->addColumn('action', function ($row) {
            return '<a href="' . route('admin.employees.edit', [$row->id]) . '" class="btn btn-info btn-circle"
                  data-toggle="tooltip" data-original-title="Edit"><i class="fa fa-pencil" aria-hidden="true"></i></a>

                  <a href="' . route('admin.employees.show', [$row->id]) . '" class="btn btn-success btn-circle"
                  data-toggle="tooltip" data-original-title="View Employee Details"><i class="fa fa-search" aria-hidden="true"></i></a>

                  <a href="javascript:;" class="btn btn-danger btn-circle sa-params"
                  data-toggle="tooltip" data-user-id="' . $row->id . '" data-original-title="Delete"><i class="fa fa-times" aria-hidden="true"></i></a>';
        });
}

请指导我如何进行。

2 个答案:

答案 0 :(得分:0)

我认为您忘记了调用数据表的make()方法。因此,您的代码应类似于:

return Datatables::of($guards)
    ->addColumn(...)
    ->make();

希望它会有所帮助:)

答案 1 :(得分:0)

public function data() {

$gurads = GuardDetails::all();

return Datatables::of($gurads)
    ->addColumn('action', function ($row) {
        return '<a href="' . route('admin.employees.edit', [$row->id]) . '" class="btn btn-info btn-circle"
              data-toggle="tooltip" data-original-title="Edit"><i class="fa fa-pencil" aria-hidden="true"></i></a>

              <a href="' . route('admin.employees.show', [$row->id]) . '" class="btn btn-success btn-circle"
              data-toggle="tooltip" data-original-title="View Employee Details"><i class="fa fa-search" aria-hidden="true"></i></a>

              <a href="javascript:;" class="btn btn-danger btn-circle sa-params"
              data-toggle="tooltip" data-user-id="' . $row->id . '" data-original-title="Delete"><i class="fa fa-times" aria-hidden="true"></i></a>';
    })->make(true);

}

您忘记添加->make(true)