yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 859 forks source link

how to fix ('Undefined index: columns') #39

Closed jongkon closed 9 years ago

jongkon commented 9 years ago

production.ERROR: exception 'ErrorException' with message 'Undefined index: columns' in vendor/yajra/laravel-datatables-oracle/src/yajra/Datatables/Engine/BaseEngine.php:919

yajra commented 9 years ago

can you please provide your js and controller code or any details to reproduce the error?

stojankukrika commented 9 years ago

I have a same error. Controller code:

 $news = News::select(array('id', 'title', 'created_at'));
  return Datatables::of($news)
            ->add_column('actions', '<a href="{{{ URL::to(\'admin/news/\' . $id . \'/edit\' ) }}}" class="btn btn-success btn-sm" > <i class="fa fa-pencil-square-o "></i>  {{ trans("table.edit") }}</a><a href="{{{ URL::to(\'admin/news/\' . $id . \'/show\' ) }}}" class="btn btn-primary btn-sm" ><i class="fa fa-eye"></i>  {{ trans("table.details") }}</a><a href="{{{ URL::to(\'admin/news/\' . $id . \'/delete\' ) }}}" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> {{ trans("table.delete") }}</a>')
            ->remove_column('id')
            ->make();

and js file:

var oTable;
    $(document).ready(function() {
        oTable = $('#data').dataTable( {
            "sDom": "<'row'<'col-md-6'l><'col-md-6'f>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
            "sPaginationType": "bootstrap",
            "oLanguage" : {
                "sProcessing":   "{{{ trans('table.processing') }}}",
                "sLengthMenu":   "{{{ trans('table.showmenu') }}}",
                "sZeroRecords":  "{{{ trans('table.noresult') }}}",
                "sInfo":         "{{{ trans('table.show') }}}",
                "sEmptyTable":   "{{{ trans('table.emptytable') }}}",
                "sInfoEmpty":    "{{{ trans('table.view') }}}",
                "sInfoFiltered": "{{{ trans('table.filter') }}}",
                "sInfoPostFix":  "",
                "sSearch":       "{{{ trans('table.search') }}}:",
                "sUrl":          "",
                "oPaginate": {
                    "sFirst":    "{{{ trans('table.start') }}}",
                    "sPrevious": "{{{ trans('table.prev') }}}",
                    "sNext":     "{{{ trans('table.next') }}}",
                    "sLast":     "{{{ trans('table.last') }}}"
                }
            },
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "{{ URL::to('admin/'.$type.'/data') }}"
        });
    });
yajra commented 9 years ago

I see. You are still using the legacy code for DT. Please use the new convention since your using v5.x.

On the other hand, if you don't like to change your js then use v4.x instead for Laravel 5 and v3.x for Laravel 4.

Below is an example of new convention for DT1.10.

$(function() {
        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: 'users-data'
        });
    });
yajra commented 9 years ago

If you need some working codes, you can also check the demo app I created for this package.

stojankukrika commented 9 years ago

TNX. Now it's works. I just change bProcessing=>processing, bServerSide=>serverSide, sAjaxSource=>ajax, dataTable=>DataTable. Thank you very much.

jongkon commented 9 years ago

Thank you very much

vijaysebastian commented 9 years ago

Here is my controller
public function getAddEditRemoveColumnData(Article $article) { $users = Article::select(['id', 'name', 'created_at', 'updated_at'])->get(); return Datatables::of($users) ->addColumn('action', function ($users) { return '&lta href="#edit-' . $users->id . '" class="btn btn-xs btn-primary"&gt&lti class="glyphicon glyphicon-edit"&gt&lt/i&gt Edit&lt/a&gt'; }) ->editColumn('id', 'ID: @{{$id}}') ->removeColumn('password') ->make(true); }

vijaysebastian commented 9 years ago

here is my js

vijaysebastian commented 9 years ago
$('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{{ url("get-articles") }}',
        columns: [
            {data: 'id', name: 'id'},
            {data: 'created_at', name: 'created_at'},
            {data: 'updated_at', name: 'updated_at'},
            {data: 'action', name: 'action', orderable: false, searchable: false}
        ]
    });
yajra commented 9 years ago

@vijaysebastian having same issue?

There are some errors on your controller. Password is not on selected fields so why remove it? Also use proper html markup on you action column and not &lt or &gt.

public function getAddEditRemoveColumnData(Article $article) {
    $users = Article::select(['id', 'name', 'created_at', 'updated_at'])->get();

    return Datatables::of($users)
    ->addColumn('action', function ($users) {
        return '<href="#edit-' . $users->id . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i>Edit</a>';
    })
    ->editColumn('id', 'ID: @{{$id}}')
    ->make(true);
}
vijaysebastian commented 9 years ago

thanks for your quick response....

yajra commented 9 years ago

Also you can remove the get() method on you Article model to use Eloquent instead of Collection:

$users = Article::select(['id', 'name', 'created_at', 'updated_at'])->get();
// should be
$users = Article::select(['id', 'name', 'created_at', 'updated_at']);
vijaysebastian commented 9 years ago

still shows same error

yajra commented 9 years ago

what version are you using for this package and Laravel?

vijaysebastian commented 9 years ago

laravel 5.0.28

vijaysebastian commented 9 years ago

image

yajra commented 9 years ago

You need to remove the get() method and use Eloquent instead. And also, you do not access the url directly. Some parameters must be set for the package to work. You need a view where you will define your table markup and js that will call the package via ajax request.

yajra commented 9 years ago

The package must work side by side with the datatables.js plugin. See this example code from their site for reference. http://datatables.net/examples/ajax/objects.html

vijaysebastian commented 9 years ago

sorry.......... so far i couldn't figure-out the problem. When i execute - dd($datatable->of($users)); shows expected output, but i got the exception while calling - return $datatable->of($users)->make(); please help me... :-(

yajra commented 9 years ago

I suggest you check/review the Demo App I created on how to make this work. Anyways, below is another example with view and controller. Example View:

<table id="users" class="table table-hover table-condensed">
    <thead>
        <tr>
            <th class="col-md-3">{{{ Lang::get('users/table.username') }}}</th>
            <th class="col-md-3">{{{ Lang::get('users/table.email') }}}</th>
            <th class="col-md-3">{{{ Lang::get('users/table.created_at') }}}</th>
            <th class="col-md-3">{{{ Lang::get('table.actions') }}}</th>
        </tr>
    </thead>
</table>

<script type="text/javascript">
$(document).ready(function() {
    oTable = $('#users').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "/users/data",
        "columns": [
            {data: 'username', name: 'username'},
            {data: 'email', name: 'email'},
            {data: 'created_at', name: 'created_at'},
            {data: 'action', name: 'action', orderable: false, searchable: false}
        ]
    });
});
</script>

Controller:

public function getData()
{
    $users = User::select(['id','name','email','created_at','updated_at']);

    return Datatables::of($users)
        ->addColumn('action', 'action here')
        ->make(true);
}
vijaysebastian commented 9 years ago

This time i just copied your code... both view and controller

yajra commented 9 years ago

And still having the same error? maybe your vendor folder is somewhat changed/corrupted? try deleting the vendor folder and run composer update to reinstall the packages?

shijupoothotta commented 9 years ago

Getting the same error reported by Vijaybastin: ErrorException in BaseEngine.php line 969: Undefined index: columns

Controller Code: public function getData() { $providers = Provider::select(['email','type','status']); return Datatables::of($providers) ->make(true);
}

shijupoothotta commented 9 years ago

After debugging with firebug, I was able to get data but this warning is shown: DataTables warning: table id=users-table - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

yajra commented 9 years ago

You might have initialized datatables more than once. Verify if you have no duplicates in your js

ghost commented 9 years ago

yajra OH no I all function replace :(

yajra commented 9 years ago

@intrepidtr is this related to update your JS code to DT1.10? If yes, you can still use v4.x so that it will work on legacy code of DT. However, new features is not available on that version.

Orthophilos commented 9 years ago

Thanks so much . You save me from a headache!!

asasmoyo commented 9 years ago

hi, i have similar error.

ErrorException in BaseEngine.php line 966:
Undefined index: columns

all i did just follow the wiki https://github.com/yajra/laravel-datatables/wiki/Installation and https://github.com/yajra/laravel-datatables/wiki/Quick-Start. my laravel version is 5.0.* and my datatables version is ~5.0. how can i fix this? thanks :D

yajra commented 9 years ago

undefined index: columns error is related to your js columns definition. Make sure that the columns defined in your js has a corresponding selected fields on your database.

columns: [
            {data: 'id', name: 'id'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'created_at', name: 'created_at'},
            {data: 'updated_at', name: 'updated_at'}
        ]
asasmoyo commented 9 years ago

my bad, it is working now, thanks a lot :D but now i have another problem, sometimes when datatables makes a request to get data, it returns 401 unauthorized, it somehow log myself out. maybe there are bugs on my middleware but i've tested it on my other controllers it is working fine

yajra commented 9 years ago

For the logout part, there is a pending issue at #60. I can't replicate that issue and still trying to figure out what may have been causing the random logout.

omar-faruk-sust commented 9 years ago

Hello, i am getting this error. error when i am using make() function this error is happening. i am using laravel 5.1 and i have a confusion about one thing. in your documentation you said to add Facade like this.

Laravel 5 'Datatables' => 'yajra\Datatables\Facades\Datatables', Laravel 5.1 'Datatables' => yajra\Datatables\Facades\Datatables::class,

currently i am adding 'Datatables' => 'yajra\Datatables\Facades\Datatables', this one to the Facade.

my code in view

in the controller $ingredients = Ingredient::select([ 'title_en','title_fr', 'is_allergen','is_sugar', 'created_at', 'updated_at']); return Datatables::of($ingredients)->make(true);

bernier commented 9 years ago

same error as omar-faruk-sust I have yajra/laravel-datatables-oracle (v5.4.1) installed

yajra commented 9 years ago

@omar-faruk-sust ,@bernier, undefined index column error means you are using the legacy code of datatables. You should use the new convention like below and DT version must be v1.10++

$('table').DataTable({
  serverSide: true,
  processing: true,
  ajax: 'data',
  columns: [
            {data: 'id', name: 'id'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'created_at', name: 'created_at'},
            {data: 'updated_at', name: 'updated_at'}
        ]
})
omar-faruk-sust commented 9 years ago

@bernier i have solve this problem. it was for the legacy code of the datatables. @yajra i have an another question? Is it responsive? how can i make it responsive?

and i am trying to join 3 table by query builder. my result is ok. but getting an error of data tables. here is my code. // for sort of join query $users = DB::table('users') ->join('role_user', 'users.id', '=', 'role_user.user_id') ->join('roles', 'roles.id', '=', 'role_user.role_id') ->select('users.*', 'roles.name as role') ->get(); //dd($users); return Datatables::of($users) ->addColumn('action', function ($user) { $actions = ''; if (Auth::user()->can('view-users')) { $actions .= ' View'; } if (Auth::user()->can('edit-users')) { $actions .= ' Edit'; } return $actions; }) ->editColumn('avatar', function($user) { if(empty($user->avatar)) { return ''; } return ''; }) ->editColumn('user_role', function ($user) { return $user->role; }) ->editColumn('created_at', function ($user) { return $user->created_at->format('Y/m/d'); }) ->editColumn('updated_at', function ($user) { return $user->updated_at->format('Y/m/d'); }) ->make(true);

but getting an error of FatalErrorException in EloquentEngine.php line 24: Call to a member function getQuery() on array

yajra commented 9 years ago

What do you mean by responsive? You need to use bootstrap 3 or css frameworks to make the design responsive and is not part of the package.

For "but getting an error of FatalErrorException in EloquentEngine.php line 24: Call to a member function getQuery() on array". There must be a bug in the package. Can you please elaborate how to reproduce the error?

omar-faruk-sust commented 9 years ago

yah i an using the bootstrap 3. i have solved this (error of FatalErrorException in EloquentEngine.php line 24: Call to a member function getQuery() on array). but i am getting another problem in search option. Search option is not working for the join query. help me!