I am facing an issue with some buttons in Laravel when using ajax to update a record using DataTables and ajax.
This is the Scripts within the blade file
<script>
$(function() {
var table=$('#roll-table').DataTable({
autoWidth: false,
processing: true,
serverSide: true,
stateSave: true,
ajax: '{{ route('getCurrentRoll') }}',
columns: [
{ data: 'action', name: 'action', orderable: false, searchable: false, "width": "25%"},
{ data: 'member.membership_number'},
{ data: 'member.first_name'},
{ data: 'member.last_name'},
{ data: 'rollstatus.status'},
{ data: 'account', render: $.fn.dataTable.render.number(',', '.', 2, '$')},
],
});
})
function updateStatus(id, status){
$.ajax({
url: '{{ route('updateStatus') }}',
type: 'POST',
data: {id: id, status: status},
datatype: 'JSON',
success: function(response){
if(response.success){
$('#roll-table').DataTable().ajax.reload();
toastr.success(response.message);
}
else{
toastr.error(response.message);
}
}
});
}
</script>
This is the controller
public function rollstatus(Request $request)
{
$r = Roll::find($request->id);
$points = Pointsmaster::where('Reason', '=', 'Attendance')->value('Value');
$member = Roll::where('id', '=', $request->id)->value('member_id');
$year = Carbon::parse(now())->year;
$rollid = RollMapping::latest()->value('id');
$status = $request->status;
switch ($status) {
// Define variables for member paying using account and check balance
case 'V':
// Check Account Balance and back out if account balance is too low
if($r->member->Accounts->sum('amount') < 10)
{
Alert()->error("Error", "Insufficient Account Balance")->autoclose(1500);
return redirect(action('RollController@index'));
}
$paid = 'Y';
$title = 'Member Present';
$message = 'Member paid using account balance';
// Add Voucher use record
$voucher = new Accounts();
$voucher->member_id = $r->member_id;
$voucher->Reason = 'Weekly Subs';
$voucher->amount = -10;
$voucher->user = Auth::user()->username;
$voucher->save();
break;
// Define variables for member who didn't pay
case 'P':
$paid = 'N';
$title = "Member Present";
$message = "Member has not paid";
break;
// Define variables for member who is online
case 'O':
$paid = 'N';
$title = "Member Online";
$message = "Member marked as present online";
break;
// Define variables for member who paid cash
case 'C':
$paid = "Y";
$title = "Member Present";
$message = "Member paid by Cash";
break;
default:
Alert()->error("Error", "System Error has occured")->autoclose(1500);
return redirect(action('RollController@index'));
}
$r->status = $status;
if($paid != 'N')
{
$r->paidrollid = $rollid;
}
$r->save();
if (config('global.Squadron_Points') != 'N')
{
$p=new Points();
$p->member_id = $member;
$p->value = $points;
$p->year = $year;
$p->reason = "Squadron Night Attendance";
$p->save();
}
alert()->success($title, $message)->autoclose(1500);
return response()->json($request->id);
}
public function getCurrentRoll(Request $request)
{
$rollid = RollMapping::latest()->value('id');
$roll = Roll::with(array('member' => function($q) {
return $q->orderby('rank');
}))
->where('roll_id', '=', $rollid)->orderby('status')
->with ('rollstatus')
->get();
return DataTables::of($roll)
->addColumn('account', function($roll) {
return $roll->member->Accounts->sum('amount');
})
->addColumn('action', function($row){
$btn = '<a href="'.action('MembersController@show', $row->member_id).'" target="_blank" title="View" class="btn btn-round btn-info"><i class="fa fa-info"></i></a>';
if ($row->status == 'A')
{
$btn .= '<a href="javascript:void(0)" data-toggle="tooltip" title="Paid Cash" class="btn btn-round btn-success" onClick="updateStatus('.$row->id.', "C")><i class="fa fa-check"></i></a>';
$btn .= '<a href="'.action('RollController@rollstatus', [$row->id, 'P']).'" class="btn btn-round btn-danger" title="Not Paid"><i class="fa fa-times"></i></a>';
$btn .= '<a href="'.action('RollController@rollstatus', [$row->id, 'V']).'" class="btn btn-round btn-warning" title="Account Payment"><i class="fa fa-money"></i></a>';
}
return $btn;
})
->make(true);
}
public function index_test()
{
//
$rollid = RollMapping::latest()->value('id');
$rolldate = RollMapping::latest()->value('roll_date');
$members = Roll::with(array('member' => function($q) {
return $q->orderby('rank');
}))
->where('roll_id', '=', $rollid)->orderby('status')->get();
$online = Settings::where('setting', 'Online Meetings')->value('value');
return view('roll.index_test', compact('members', 'rolldate', 'rollid', 'online'));
}
rollstatus is the function I wish to call when updating the record
index_text loads the view
getCurrentRoll builds the DataTable
- Please note I have only updated the first button when after checking the status
if ($row->status == 'A')
{
$btn .= '<a href="javascript:void(0)" data-toggle="tooltip" title="Paid Cash" class="btn btn-round btn-success" onClick="updateStatus('.$row->id.', "C")><i class="fa fa-check"></i></a>';
Once this one is working I can update the others.
The issue I am facing is that when I click on the Paid Cash button, I get a
Uncaught SyntaxError: Unexpected end of input
error in my console
What I am trying to do is pass the Row ID and “P” for paid into the controller
I have the following for my Route
Route::post('post/roll/updateStatus', 'RollController@rollstatus')->name('updateStatus');
I would like to know where I have gone wrong, also if I can have the record updated without refreshing the whole table (so if I have a searched filter, this remains but the record is updated)
Thanks