I was perfectly implement dataTable in my laravel project.For this i followed some tutorial data doesn't show but again and again showing "DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see https://datatables.net/tn/7". I dont understand how to fix this.
In the console following error showing
Could you tell me where is my problem.
My Controller Code
use App\Models\Totalmember;
use Yajra\DataTables\DataTables;
public function totalviewmembers(Request $request)
{
if ($request->ajax()) {
$data = Totalmember::select('membername', 'cellnumber', 'branchname', 'presentaddress', 'uniqueid');
//echo $data;
return DataTables::of($data)
->addColumn('action', function ($row) {
return '<a href="'.url('edit/'.$row->uniqueid).'" class="btn btn-sm btn-primary">Edit</a>';
})
->rawColumns(['action'])
->make(true);
}
// Load the view
return view('admin.totalmember.viewtotalmembers');
}
Route
Route::get('/viewmember', [TotalmembersController::class, 'totalviewmembers']);
This is my blade coding
<table class="table table-bordered data-table">
<thead>
<tr>
<th>Name</th>
<th>Mobile Number</th>
<th>Branch Name</th>
<th>Present Address</th>
<th style="display: none;">Unique ID</th> <!-- Hidden -->
<th>Action</th>
</tr>
</thead>
</table>
<script>
$(function () {
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url('totalviewmembers') }}", // AJAX Route
columns: [
{data: 'membername', name: 'membername'},
{data: 'cellnumber', name: 'cellnumber'},
{data: 'branchname', name: 'branchname'},
{data: 'presentaddress', name: 'presentaddress'},
{
data: 'uniqueid',
name: 'uniqueid',
visible: false, // Hidden column
searchable: true // Used for search functionality
},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
});
</script>
You set the wrong route
Change to this
ajax: "{{ url('viewmember') }}",
If you still want to stick on your old name, you can name that route e.g
Route::get('/viewmember', [TotalmembersController::class, 'totalviewmembers'])->name('totalviewmembers');;