If a 'category' is added to the menu using the 'menus' tool in Wordpress, I want my theme to automatically add a submenu containing links to all of the posts in that category.

In my functions.php file I have created a function and hooked it to 'walker_nav_menu_start_el' action. By passing the $item variable into the function I am able to identify if the current menu item is a category and what category it is. But I'm having trouble creating the list of links with post titles for all posts in that category.

At first I thought I could use the 'get_posts' function and a foreach loop, but because I'm not outputting, I'm just building an output variable to be passed back to the 'walker_nav_menu_start_el' action, it's merely outputting the details of the current post instead of the details for each post it loops through. This is the code I tried:

    function hw_submenu( $output, $item ) {
       if( $item->object == 'category' ) {
    
          $hw_output = '<ul>';
          $subposts = get_posts( array(
             'posts_per_page' => -1,
             'category' => $item->object_id
          ) );
          if ( $subposts ) {
             foreach ( $subposts as $post ) :
                setup_postdata( $post );
                $hw_output .= "<li><a href='" . the_permalink() . "''>" . the_title('', '', false) . "</a></li>";
             endforeach; 
             wp_reset_postdata();
          }
    
          $output .= $hw_output . "</ul>";
       }    
    
       return $output;
    }
    add_action( 'walker_nav_menu_start_el', 'hw_submenu', 10, 2);

And this is an example menu output:

    -Home //page
    -About //page
    -Animals //category
     +Pig
     +Pig //this is the post I am currently viewing - there are 2 posts in the animals category though, so that bit is correct
    -Vehicles //category
     +Pig
     +Pig
     +Pig
     +Pig //this is the post I am currently viewing - there are 4 posts in the animals category though, so that bit is correct

How can I get each category to list its posts in the menu? Can I do it with this action hook? Or should I be looking at a custom walker?

Thanks,
Josh