On the blade I have this code:
@foreach
<tr id="{{$employee->id}}">
<td class="visibleemployee">
<form action="{{route('admin.employees.cambiarVisible',$employee->id)}}">
<button type="button" id="buttonchangevisible" data-id="{{$employee->id}}">
@if ($employee->public == '1')
<i class="fa fa-check" aria-hidden="true" id="margindataemployee" class="cambiarsiporno"></i>
@else
<i class="fa fa-times" aria-hidden="true" id="margindataemployee"></i>
@endif
</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</td>
</tr>
When you click on the button, this function is executed with Ajax:
$("#buttonchangevisible").click(function(e){
e.preventDefault();
var button = $(this);
var id = button.data('id');
var formData = new FormData($(this)[0]);
$.ajax({
url:'employee/cambiarVisible/' + id,
type: 'PUT',
data: formData,
success: function() {
location.reload();
},
cache: false,
contentType: false,
processData: false
});
return false;
});
Calling the following controller method:
public function cambiarVisible(Request $request, $id)
{
$employee = Worker::findOrFail($id);
$employee->public = ($employee->public == 1) ? 0 : 1;
$employee->save();
}
File routes: (everything goes inside a group that adds the prefix admin).
Route::put('employee/cambiarVisible/{id}', ['uses' => 'AdminController@cambiarVisible', 'as' => 'admin.employees.cambiarVisible']);
The error cited in the title is solved, now clicking on the first row makes the call correctly but does not update the field. Clicking on any other row directly does not make the call.