0

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')
1
  • 1
    1. check data is coming from controller by putting console.log (data) in success function Commented Dec 26, 2019 at 15:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0
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);
}

1 Comment

Explaining why/how your code solves the issue posted by OP would be more helpful to future visitors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.