I was following this tutorial to update or edit database table in Laravel. I have implemented the insertion section as follows:
$(document).on('click', '.edit', function(){
var id = $(this).attr("id");
$('#form_output').html('');
$.ajax({
url:"{{route('ajax_data_manage.fetchdata')}}",
method:'get',
data:{id:id},
dataType:'json',
success:function(data)
{
$('#player_name').val(data.player_name);
$('#player_country').val(data.player_country);
$('#player_age').val(data.player_age);
$('#player_id').val(id);
$('#playerModal').modal('show');
$('#action').val('Edit');
$('.modal-title').text('Edit Data');
$('#button_action').val('update');
}
})
});
in controller file:
function fetchdata(Request $request)
{
$id = $request->input('id');
$player = Player::find($id);
$output = array(
'player_name' => $player->player_name,
'player_country' => $player->player_country,
'player_age' => $player->player_age
);
echo json_encode($output);
}
I think the error is in fetching the data. My Player table in the database has three columns (name, country, age) and the id and name of text-box in the form group:
<div class="form-group">
<label>Enter Player Name</label>
<input type="text" name="player_name" id="player_name" class="form-control" />
</div>
<div class="form-group">
<label>Enter Player Country</label>
<input type="text" name="player_country" id="player_country" class="form-control" />
</div>
<div class="form-group">
<label>Enter Player Age</label>
<input type="text" name="player_age" id="player_age" class="form-control" />
</div>
when I click the edit button, nothing is happen. I have also edited the web.php file
Route::get('ajaxdatamanage','AdminController@index')->name('ajax_data_manage');
Route::get('ajaxdatamanage/getdata', 'AdminController@getdata')->name('ajax_data_manage.getdata');
Route::post('ajaxdatamanage/postdata', 'AdminController@postdata')->name('ajax_data_manage.postdata');
Route::get('ajaxdatamanage/fetchdata', 'AdminController@fetchdata')->name('ajax_data_manage.fetchdata')