Drupal - Set parent for programmatically created menu links
Turns out when setting the parent you don't use the id of the menu_link_content you use the 'menu_link_content:' followed by the uuid of the link. Here's an example of a properly formatted parent field.
'menu_link_content:0025c274-4db3-4745-b51e-714c998b5a50'
I fixed the code like so
foreach ($items as $id => $title) {
if ($id == '2') {
$parent = 'menu_link_content:' . $previous_menu_link->uuid();
} else {
$parent == NULL;
}
$menu_link = MenuLinkContent::create([
'title' => $title,
'link' => ['uri' => 'internal:/node/' . $nid],
'menu_name' => $menu_id,
'expanded' => true,
'langcode' => $langcode,
'status' => TRUE,
'parent' => $parent,
]);
$menu_link->save();
$previous_menu_link = $menu_link;
}