Laravel/Ajax: selecting a row with a different id but same group_id shows a different value

  ajax, controller, forms, laravel, sql

To explain my title, this is my table, it has multiple rows with the same group_id but different id

1

Here are those same rows in my output: (I added their id beside the names to show the value)

2

My problem is that when I click on for instance, the edit button on the first row id=41 group_id=1, my text fields show the values for id=1 group_id=1

Another screenshot below, the modal (edit button on id=41). I added text fields for id and group_id to show which row is retrieved from the table. As you can see, it outputs the row of id=1 Codes are further down.

3

Routes:

use AppHttpControllersFrontendBBRCategoryConfigurationController;

Route::get('/bbr-group-configuration-edit/{id}', [BBRGroupConfigurationController::class,'edit']);

Controller: (my table is retrieved from HmsBbrGroup)

public function edit($id) {
    $group_edit = HmsBbrGroup::find($id);
    if ($group_edit) 
    {
        return response()->json([
            'status'=>200,
            'group_edit'=>$group_edit,
        ]);
    }
    else
    {
        return response()->json([
            'status'=>404,
            'message'=>'Group Not Found',
        ]);
    }
}

Ajax:

        $(document).on('click','.edit_group',function (e) {
            e.preventDefault();
            $(".save_button").removeClass("add_group").addClass("update_group");
            var g_id = $(this).val();
            console.log(g_id);
            $('#group_form').modal('show');
            $.ajax({
                type: "GET",
                url: "/clinical/bbr-group-configuration-edit/"+g_id,
                success: function (response) {
                     console.log(response);
                    if(response.status == 404) {
                        $('#success_message').html("");
                        $('#success_message').addClass('alert alert-danger');
                        $('#success_message').text('response.message');
                    } else {
                        $('#id').val(response.group_edit.id);
                        $('#group_id').val(response.group_edit.group_id);
                        $('#group_name').val(response.group_edit.group_name);
                        $('#group_description').val(response.group_edit.group_description);

                        $('#effective_start_datetime').val(response.group_edit.effective_start_datetime ? 'active' : 'inactive').change();
                        $('#group_type_id').val(response.group_edit.group_type_id).change();
                    }
                }
            });
        });

Forms:

   <input type="text" id="id" class="id">
   <input type="text" id="group_id" class="group_id">
   <input type="text" id="group_name" class="group_name" placeholder="Group Name">
   <textarea id="group_description" class="group_description" placeholder="Group Description">
   <select id="effective_start_datetime" class="effective_start_datetime">
   <select id="effective_start_datetime" class="effective_start_datetime">

Things to note:

  • I am trying to retrieve the right row so I made my route get the id:
    /bbr-group-configuration-edit/{id}
  • What my ajax does is to show the values of the row in the modal (3rd screenshot), specifically in the else statement
  • It could have something to do with my controller $group_edit = HmsBbrGroup::find($id); but I cannot figure out why it takes the first (or lowest) value id within the same group_id

I explained in detail everything that has been done so far, hope I can get some help with my issue. Thanks for reading.

TLDR: wrong row is shown when retrieving from the table (edit form)

Source: Laravel

Leave a Reply