Laravel:为 JQuery 中的每个添加一秒结果只显示 "undefined"

Laravel: adding a second for each in JQuery results in just showing "undefined"

我将首先展示所讨论的每个问题的所有必要代码(每个当前正在工作的代码都在另一个代码中)

型号:

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

Table:

控制器:(已翻译SQL查询)

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,
    ]);
}

每个代码:

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

注意事项:

有问题的代码是:

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

如我上面的 SQL 查询所示:all_group_users 具有适当的输出。我想知道为什么将它放在 all_groups.

的功能 for each 中时显示为 undefined

我希望这个解释足够了,如果我需要再详细说明,请随时告诉我。感谢您的帮助。

你必须这样做:

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

这是我更改的部分:

'<td>';

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