Laravel-datatables: Error: DataTables warning: table id=post - Ajax error. For more information about this error, please see http://datatables.net/tn/7

Created on 21 Apr 2017  路  9Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

Code snippet of problem

System details

  • Operating System Windows 10
  • PHP Version 7.1.1
  • Laravel Version 5.3
  • Laravel-Datatables Version 6.0

i always got this error
here is my code
<div class="table-responsive">
<table id="post" class="table">
<thead>
<th>id</th>
<th>title</th>
<th>content</th>
<th>created_at</th>
<th>Action</th>
</thead>
</table>
</div>

$(function() {
$('#post').DataTable({
processing: true,
ServerSide: true,
ajax: '{{ url('posts/data') }}',
columns: [
{ data: 'id' },
{ data: 'title' },
{ data: 'content' },
{ data: 'created_at' },
{ data: 'action', orderable: false, searchable: false }
]
});
});
$.fn.dataTable.ext.errMode = 'throw';

public function dataPost(Request $request) {
if($request->ajax()){
$posts = Post::select('id', 'title', 'content', 'created_at')
->get();
return Datatables::of($posts)
->addColumn('action', function ($id) {
return '<a href="guest/' . $id->slug . '" class="btn btn-primary">Lihat</a>';
})
->make(true);
} else {
exit("Not an AJAX request -_-");
}
}

Route::get('posts/data', 'PostController@data');

pls help me !_!

question

Most helpful comment

@alko79 as the error states, you have an invalid sql. Maybe use regs.large instead?

All 9 comments

Try inspecting the ajax response to get a hint of what's causing the error. Use developer tools and network tab.

same issue for me, inspecting i had this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reg.large' in 'order clause' (SQL: select regs.* from regs order by reg.large asc limit 10 offset 0)

this is code :

public function ajax(){
  return $this->datatables
          ->eloquent($this->query())
...
...
->addColumn('dimensioni', function($production) { $dimension=$production->lenght."x".$production->large."x".$production->height; return $dimension ;})
...
...       
 ->orderColumn('updated', '-updated $1')
        ->escapeColumns([])
        ->make(true);
}
protected function getColumns(){
        return[...
...
['data' => 'dimension', 'name' => 'reg.large', 'title' => 'Measure','orderable'=>true],
...
];
}



md5-2f2650190afbbc890cc43af0fb299d1f



       $regs =Reg::query()->with('comm')->select('regs.*');
       return $this->applyScopes($regs);

}

@alko79 as the error states, you have an invalid sql. Maybe use regs.large instead?

@yajra yes that was the typo error thank you so much it was so trivial, in the end my fault , @harismiftahulhudha have you same error code (SQLSTATE[42S22]: Column not found: 1054) ?

Yeah, this error is a bit trivial hence I am trying to add an error handler that will customize this alert error into the actual exception message. PR https://github.com/yajra/laravel-datatables/pull/1131 should help us lessen this kind of issue.

Hey, guys!
I'm having the same problem here. I'm using datatables as a service and when I try to sort by user role, I'm getting this error: "Invalid column name 'roles'."

public function dataTable()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->addColumn('display_name', function (User $user) {
                return $user->roles->map(function ($role) {
                    return $role->display_name;
                })->implode(', ');
            })
    }

public function query()
    {
        $query = User::with('roles')->select([
            'id',
            'email',
            'created_at',
        ]);

        return $this->applyScopes($query);
    }

public function html()
    {
        return $this->builder()
                    ->columns([
                                         'id',
                                         'email',
                                         'roles.display_name',
                                         'created_at'
                        ])
                    ->parameters($this->getBuilderParameters());
    }

The query executed is:
SQL: select top 10 [id], [username], [real_name], [email], [created_at], [roles].[roles] from [users]...
so instead of [roles].[display_name] it gets [roles].[roles].

Any ideea?

excuse me i have some error on datatables too , this is my code in controller
BooksController.php. I have try on other discuss but not worked yet
` public function index(Request $request, Builder $htmlBuilder)
{
if ($request->ajax()) {
$books = Book::with('author');
return Datatables::of($books)
->addColumn('action', function($book){
return view('datatable._action', [
'model' => $book,
'form_url' => route('books.destroy', $book->id),
'edit_url' => route('books.edit', $book->id),
'confirm_message' => 'Yakin mau menghapus ' . $book->title . '?'
]);
})->make(true);
}

    $html = $htmlBuilder
        ->addColumn(['data' => 'title', 'name'=>'title', 'title'=>'Judul'])
        ->addColumn(['data' => 'amount', 'name'=>'amount', 'title'=>'Jumlah'])
        ->addColumn(['data' => 'author.name', 'name'=>'author.name', 'title'=>'Penulis'])
        ->addColumn(['data' => 'action', 'name'=>'action', 'title'=>'', 'orderable'=>false, 'searchable'=>false]);

    return view('books.index')->with(compact('html'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}

}

`
capture

please help me , thanks

Hey, guys!
I'm having the same problem here. I'm using datatables as a service and when I try to sort by user role, I'm getting this error:
DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error,

I did this and I also have an 500 error when I try to change the data

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sangnguyenplus picture sangnguyenplus  路  3Comments

Abdulhmid picture Abdulhmid  路  3Comments

FilipeBorges1993 picture FilipeBorges1993  路  3Comments

shadoWalker89 picture shadoWalker89  路  3Comments

josiahke picture josiahke  路  3Comments