Set parent for programmatically created menu links I'm currently programmatically generating a menu and menu links inside it. This is working but I'm having trouble setting the parents for the links. Here is my current code

    $langcode = 'es';
    if (empty($menu_id)) {return ['#markup' =&gt; 'menu not specified'];}
    $menu = \Drupal::entityTypeManager()-&gt;getStorage('menu')
        -&gt;create([
            'langcode' =&gt; $langcode,
            'status' =&gt; TRUE,
            'label' =&gt; $menu_id,
            'id' =&gt; $menu_id,
            'description' =&gt; 'this menu was created programmatically',
        ])-&gt;save();

    $items = [
        '1' =&gt; 'item 1',
        '2' =&gt; 'item 2',
        '3' =&gt; 'item 3',
    ];
    $parent = NULL;
    foreach ($items as $id =&gt; $title) {
        if ($id == '2') {$parent = '1';} else { $parent == NULL;}
        $menu_link = MenuLinkContent::create([
            'title' =&gt; $title,
            'link' =&gt; ['uri' =&gt; 'internal:/node/' . $nid],
            'menu_name' =&gt; $menu_id,
            'expanded' =&gt; true,
            'langcode' =&gt; $langcode,
            'status' =&gt; TRUE,
            'parent' =&gt; $parent,
        ]);

        $menu_link-&gt;save();
        var_dump($menu_link-&gt;getParentId());
    }


This line

var_dump($menu_link-&gt;getParentId());


is actually displaying the correct parent. But when visiting the menu in the admin page the parent is not set and is at the top level of the menu. Is there a specific issue with my code? How would you set the parent of a programmatically generated menu link?
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 =&gt; $title) {
        if ($id == '2') {
            $parent = 'menu_link_content:' . $previous_menu_link-&gt;uuid();
        } else {
            $parent == NULL;
        }

        $menu_link = MenuLinkContent::create([
            'title' =&gt; $title,
            'link' =&gt; ['uri' =&gt; 'internal:/node/' . $nid],
            'menu_name' =&gt; $menu_id,
            'expanded' =&gt; true,
            'langcode' =&gt; $langcode,
            'status' =&gt; TRUE,
            'parent' =&gt; $parent,
        ]);

        $menu_link-&gt;save();
        $previous_menu_link = $menu_link;
    }