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 !_!
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)
{
//
}
}
`
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
Most helpful comment
@alko79 as the error states, you have an invalid sql. Maybe use
regs.largeinstead?