Add category to body class

I want to add the currenty category to the body class.

How do I need to modify my current code, to add also the current category as a body class to the is_singular( 'knowledgebase' ) section?

My current code looks like this:

function add_body_class( $classes ) {

    global $pakb;

    if( is_page( $pakb->get( 'kb_page' ) ) && ! is_null(  $pakb->get( 'kb_page' ) ) ) {
        $classes[] = 'pakb-main';
    } elseif ( is_tax( 'knowledgebase_category' ) ) {
        $classes[] = 'pakb-category';
    } elseif ( is_singular( 'knowledgebase' ) ) {
        $classes[] = 'pakb-single';
    }

    return $classes;

}

Solutions Collecting From Web of "Add category to body class"

This late in the process you should be able to rely on context set up, even outside of loop.

On top of my head something like this should work:

$categories = wp_get_post_categories( get_the_ID(), [ 'fields' => 'names' ] );

Here I don’t understand what is global $pakb.

But extending to Rarst answer :

add_filter('body_class', 'add_body_class');

function add_body_class($classes) {
    global $pakb;
    global $post;

    if( is_page( $pakb->get( 'kb_page' ) ) && ! is_null(  $pakb->get( 'kb_page' ) ) ) {
        $classes[] = 'pakb-main';
    } elseif ( is_singular( 'knowledgebase' ) ) {
        // getting all category name related to this post
        $categories = wp_get_post_categories( $post->ID, [ 'fields' => 'names' ] );

        $classes[] = $categories;
    }

    return $classes;

return $classes;
}