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


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


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.



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([
        return response()->json([
            'message'=>'Group Not Found',


        $(document).on('click','.edit_group',function (e) {
            var g_id = $(this).val();
                type: "GET",
                url: "/clinical/bbr-group-configuration-edit/"+g_id,
                success: function (response) {
                    if(response.status == 404) {
                        $('#success_message').addClass('alert alert-danger');
                    } else {

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


   <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:
  • 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