I am trying to use datatables of jquery to create a table with fetched members from database. This is my html and javascript code:
<table id="workerTable" class="table-bordered table-hover" width="80%" cellspacing="0">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Role</th>
<th>Dep_id</th>
<th>Start_Date</th>
<th>Updated</th>
</tr>
</thead>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#workerTable').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {{ URL::route('workerData') }}
} );
} );
</script>
I have this route defined:
Route::get('/workers/data' , 'WorkersController@fetch')->name('workerData');
And the function fetch() inside the WorkersController is like that:
public function fetch()
{
$workers = Worker::all();
echo json_encode($workers);
}
I am new to laravel and I think I am not understanding it well. Is the call to this line
"ajax": {{ URL::route('workerData') }}
make the route to call the fetch function of the WorkersController ?
"ajax": '{{ URL::route('workerData') }}'return $workers->toJson()in the fetch method