Reputation: 97
I was trying to update/change the currentStatus
of a row from 0
to 1
using the Approve
button in the datatable, but I have no idea how to do it. Can anyone please help me?
This is how I store
public function store(Request $request){
$currentStatus = 0;
$data = $request->validate([
'to' => 'required',
'date' => 'date',
'address' => 'required',
'reference' => 'required',
'attention' => 'required',
'area' => 'required',
'project' => 'required',
'salesman' => 'required',
'location' => 'required'
]);
\App\Contract::create($data + ['status' => $currentStatus]);
return redirect('contracts/pendings');
}
This is how I display in the datatables
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Customers Name</th>
<th>Date Created</th>
<th>Project Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
@forelse($pendingContracts as $pendingContract)
<td>{{$pendingContract->to}}</td>
<td>{{$pendingContract->date}}</td>
<td>{{$pendingContract->project}}</td>
<td>
<button type="button" class="btn btn-info">
<a href="/contracts/pendings/{{$pendingContract->id}}" style="color: #ffffff;">View Details</a>
</button>
<button type="button" class="btn btn-success">
<a href="" style="color: #ffffff;">Approve</a>
</button>
</td>
</tr>
@empty
<center><p>no data</p></center>
@endforelse
</tbody>
</table>
Upvotes: 1
Views: 1522
Reputation: 99
I share the sample code. Please check
View file - Button
<input data-id="{{$contract->id}}" class="toggle-class" type="checkbox" data-onstyle="success" data-offstyle="danger" data-toggle="toggle" data-on="Approve" data-off="Disapprove" {{ $contract->is_approve ? 'checked' : '' }}>
Put the below JS code in the view file
<script>
$(document).off('change', '.toggle-class').on('change', '.toggle-class', function() {
var url = 'contract/changestatus';
var is_approve = $(this).prop('checked') == true ? 1 : 0;
var contract_id = $(this).data('id');
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: {"_token": "{{ csrf_token() }}", 'is_approve': is_approve, 'contract_id': contract_id},
success: function(data){
console.log(data.success)
}
});
return false;
});
</script>
Controller code
public function changestatus(Request $request)
{
$contract = Contract::find($request->contract_id);
$contract->is_approve = $request->is_approve;
$contract->save();
return redirect()->route('admin.contract.index');
}
I hope this snippet is helpful to you. Thank you :)
Upvotes: 2