Home > front end >  Laravel: adding a second for each in JQuery results in just showing "undefined"
Laravel: adding a second for each in JQuery results in just showing "undefined"

Time:10-05

I will first show all the necessary code for the for each in question (which is inside another for each that is currently working)

Model:

class HmsBbrGroupUser extends Model
{
    public $timestamps = false;
    protected $table = 'hms_bbr_group_user';
}

Table:

1

Controller: (translated SQL query)

use App\Models\HmsBbrGroupUser;

public function fetchgroupuser() {

    $all_group_users = HmsBbrGroupUser::selectRaw('hms_bbr_group_user.id,
                                                   hms_bbr_group_user.group_id,
                                                   hms_bbr_group_user.parent_id,
                                                   hms_bbr_group_user.jid,
                                                   hms_doctor.name ')
    ->leftJoin('hms_bbr_group', 'hms_bbr_group.id', '=', 'hms_bbr_group_user.parent_id')
    ->leftJoin('hms_doctor', 'hms_doctor.jid', '=', 'hms_bbr_group_user.jid')
    ->where( 'hms_bbr_group_user.group_id', '=', 113)
    ->whereNull('hms_bbr_group.archived_by')
    ->get();

    return response()->json([
    'all_group_users'=>$all_group_users,
    ]);
}

for each code:

var tbody="";
$.each(response.all_groups, function (key, group) {
tbody ='<tr>' 
            '<td><p class="font-weight-bold mb-0">' group.group_id '</p>' group.group_description '</td>' 
            '<td>' group.group_type_name '</td>' 
            '<td>' 

            $.each(response.all_group_users, function (key, group_user) {
                '<p>' group_user.jid '</p>'
            });   
            tbody ='</td>' 

            '<td>' getgroupstatus(group.effective_start_datetime, group.effective_end_datetime) '</td>' 
            '<td>' 
                '<button type="button" value="' group.id '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> ' 
                '<button type="button" value="' group.group_id '" title="delete '  group.group_name  '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>' 
            '</td>' 
        '</tr>';
});

Things to note:

  • I translated the SQL Query on the screenshot above with proof that it is functioning to the laravel controller function.
  • I am trying to add that function as a for each in my table shown below
  • That for each inside another for each just shows undefined

2

The code in question is:

$.each(response.all_group_users, function (key, group_user) {
  '<p>' group_user.jid '</p>'
  });   
tbody ='</td>' 

As show in my SQL query above: all_group_users has the appropriate output. I am wondering why this is shown as undefined when placing it inside the functioning for each of all_groups.

I hope this explanation is enough, feel free to tell if I need to elaborate any more. Thank you for any help.

CodePudding user response:

You will have to do it like this:

var tbody = "";
$.each(response.all_groups, function(key, group) {
  tbody  = '<tr>'  
    '<td><p class="font-weight-bold mb-0">'   group.group_id   '</p>'   group.group_description   '</td>'  
    '<td>'   group.group_type_name   '</td>'  
    '<td>';

    $.each(response.all_group_users, function(key, group_user) {
      tbody  = '<p>'   group_user.jid   '</p>'
    });
    tbody  = '</td>'  

    '<td>'   getgroupstatus(group.effective_start_datetime, group.effective_end_datetime)   '</td>'  
    '<td>'  
    '<button type="button" value="'   group.id   '" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button> '  
    '<button type="button" value="'   group.group_id   '" title="delete '   group.group_name   '" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>'  
    '</td>'  
    '</tr>';
});

This is the part i've changed:

'<td>';

$.each(response.all_group_users, function(key, group_user) {
  tbody  = '<p>'   group_user.jid   '</p>'
});
tbody  = '</td>'  
  • Related