Skip to content

DataTables warning: table id=datatable - Requested unknown parameter 'id' for row 0, column 0. #2943

@pinmosarn

Description

@pinmosarn

when I return the response without using a laravel resource everything works well...
But I tried to return the resource response as the documentation says:
https://yajrabox.com/docs/laravel-datatables/master/response-resource

i get this js alert error:

DataTables warning: table id=datatable - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

Controller:

public function getData(){
        $posts = Post::paginate(10);
 
        $resource = PostResource::collection($posts);
        
        return datatables()->of($resource)->toJson();
}

the view

$(document).ready( function () {
        $('#datatable').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": "{{ route('posts.data') }}",
            "columnDefs": [
                { "searchable": true, "targets": 0 }
            ],
            "columns": [
                { data: 'id', name: 'id' },
                { data: 'title', name: 'title' },
                { data: 'created_at', name: 'created_at' },
            ]
        });
    });

the resource:

public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'created_at' => $this->created_at->format('Y-m-d'),
        ];
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions