JSON formatting error
When I try to initialize Datatable, I got something like that.

and my json response is like that.

And do I still need to call from the view like
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/api.users"
} );
} );
My controller
public function getDatatable()
{
return Datatable::collection(User::all(['name','nrc','company','email','phone','status']))
->showColumns('name','nrc','company','email','phone','status')
->searchColumns('name','nrc','company','email','phone','status')
->orderColumns('name','nrc','company','email','phone','status')
->make();
}
My view
{{ Datatable::table()
->addColumn('Name','Nrc', 'Company','Email','Phone','Status')
->setUrl(route('api.users'))
->render() }}
My datatable version is 1.9.4. Can anyone help me? I've tried to searh similar questions like me with no luck. Thanks in advance.
Looks like you have a dd or var_dump inside your code.
You should remove that to get a pure json response
Dear Chumper
No I don't have any dd or var_dump. I'll show you complete code.
My Route
Route::get('datatable', function(){
return View::make('datatable');
});
Route::get('/api/users',[
'as' => 'api.users',
'uses' => 'HomeController@getDatatable'
]);
My Controller
public function getDatatable()
{
return Datatable::collection(User::all(['name','nrc','company','email','phone','status']))
->showColumns('name','nrc','company','email','phone','status')
->searchColumns('name','nrc','company','email','phone','status')
->orderColumns('name','nrc','company','email','phone','status')
->make();
}
My View
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table</title>
{{ HTML::style('css/bootstrap.css') }}
{{ HTML::style('css/dataTable.bootstrap.css') }}
{{ HTML::script('js/jquery.js') }}
{{ HTML::script('js/bootstrap.js') }}
{{ HTML::script('js/jquery.dataTables.min.js') }}
</head>
<body>
<div class="container">
{{ Datatable::table()
->addColumn('Name','Nrc', 'Company','Email','Phone','Status')
->setUrl(route('api.users'))
->render() }}
</div>
</body>
</html>
That's it.I reference the necessary files before datatable called like you said.
Anybody please...
my tip would now to step through the code, as you cann see in the screenshot zour first lines lists the sql command. So somewhere there is a dd or var_dump on this sql command
To @webcrazy
public function getDatatable()
{
die();
}
And check again json. if Empty you try:
public function getDatatable()
{
dd(User::all(['name','nrc','company','email','phone','status']));
}
What you see???
Dear @Chumper I'm pretty sure I don't have any dd or var_dump but my controller code.
Dear @thienhung1989
With your first code, my Json response is
This request has no response data available.
with this second code

and error pop-up as well.
DataTables warning (table id='datatable': Data Tables warning: JSON data from server could not be parsed.This is caused by a JSON formatting error.
@webcrazy You check again Model "User". First line: Invaild. And if All ok, you try with Model orther by 'Method two'. If first line sample with Model 'User:All', i sure have problem in Eloquent (modified).
Good Luck!
@thienhung1989 @Chumper Thank you guys. I'll try.