Reputation: 434
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')
Upvotes: 0
Views: 407
Reputation:
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
);
// try this
$update = Player::where('id',$id)->update($output);
// or
$player->player_name;
$player->player_country;
$player->player_age;
$player->save();
echo json_encode($player);
}
Upvotes: 0
Reputation: 119
You will get full demo for this and it will help full for you.
https://github.com/PriyankPanchal/AJAX-CRUD-Laravel
Please clone project from using above link and after clone fire some command as below.
1) composer update 2) Set Up env file and set database details in .env file. 3) php artisan migrate
Hope this demo project will help you. Thanks PHPanchal
Upvotes: 1