Laravel-未设置集合中的记录Id

Laravel-未设置集合中的记录Id,laravel,eloquent,Laravel,Eloquent,今天,我在从表中获取记录时,在Laravel中遇到了一些非常奇怪的行为。我有一张桌子,它本身有一对多的关系。一个菜单项可以有多个子菜单。以下是简要结构: Table name: site_navigation id => Primary Key, Auto Increment parent_id => Index, Defaults Null menu_name => Varchar “$arrMainMenu”仅包含集合的最后一条记录当我试图打印$menu->i

今天,我在从表中获取记录时,在Laravel中遇到了一些非常奇怪的行为。我有一张桌子,它本身有一对多的关系。一个菜单项可以有多个子菜单。以下是简要结构:

Table name: site_navigation

id  => Primary Key, Auto Increment
parent_id   => Index, Defaults Null
menu_name   => Varchar
“$arrMainMenu”仅包含集合的最后一条记录当我试图打印$menu->id时,它打印为空白。即使子记录也不会打印它的菜单id。即使我使用下面的代码,仍然没有输出表示未设置“id”属性

@foreach($collSiteMenu as $menu)          
          @isset($menu->id)
            {{ 'menu id is set to '. $menu->id }}
          @endisset
@endforeach
但是,在控制器中,当我添加($collSiteMenu)时,它会打印以下内容:

Collection {#380 ▼
  #items: array:12 [▼
    0 => Sitemenu_model {#393 ▼
      #table: "site_navigation"
      #fillable: array:10 [▶]
      +id: null
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:13 [▼
        "id" => 1
        "menu_name" => "About Us"
        "parent_id" => null
        "url_slug" => "about-us"
        "canonical_url" => null
        "show_in_nav" => "show_both"
        "meta_title" => "About Us"
        "meta_keywords" => null
        "meta_description" => "this is test description"
        "is_active" => "yes"
        "display_order" => 1
        "created_at" => "2020-09-02 12:39:20"
        "updated_at" => "2020-09-04 16:21:51"
      ]
      #original: array:13 [▼
        "id" => 1
        "menu_name" => "About Us"
        "parent_id" => null
        "url_slug" => "about-us"
        "canonical_url" => null
        "show_in_nav" => "show_both"
        "meta_title" => "About Us"
        "meta_keywords" => null
        "meta_description" => "this is test description"
        "is_active" => "yes"
        "display_order" => 1
        "created_at" => "2020-09-02 12:39:20"
        "updated_at" => "2020-09-04 16:21:51"
      ]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "getChildren" => Collection {#411 ▼
          #items: array:2 [▼
            0 => Sitemenu_model {#415 ▼
              #table: "site_navigation"
              #fillable: array:10 [▶]
              +id: null
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:13 [▶]
              #original: array:13 [▼
                "id" => 7
                "menu_name" => "Who We Are"
                "parent_id" => 1
                "url_slug" => "who-we-are"
                "canonical_url" => null
                "show_in_nav" => null
                "meta_title" => "Who We Are"
                "meta_keywords" => null
                "meta_description" => null
                "is_active" => "yes"
                "display_order" => 1
                "created_at" => "2020-09-02 14:14:03"
                "updated_at" => "2020-09-02 14:14:03"
              ]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
            1 => Sitemenu_model {#416 ▼
              #table: "site_navigation"
              #fillable: array:10 [▶]
              +id: null
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:13 [▶]
              #original: array:13 [▼
                "id" => 8
                "menu_name" => "Our Cause"
                "parent_id" => 1
                "url_slug" => "our-cause"
                "canonical_url" => null
                "show_in_nav" => null
                "meta_title" => "Our Cause"
                "meta_keywords" => null
                "meta_description" => null
                "is_active" => "yes"
                "display_order" => 2
                "created_at" => "2020-09-02 14:15:27"
                "updated_at" => "2020-09-02 14:15:27"
              ]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
我在谷歌上搜索过,但找不到解决办法。谁能告诉我哪里出了问题

多谢各位


Javed

哪里是
getChildren
model,请给我看getChildren模型?@EncangCutbray,此方法在“Sitemenu\u model”模型本身中定义。请向上滚动我的问题,您会找到它。如果您只返回$collsite菜单,您会得到什么?这看起来像是一个为您提供嵌套集合功能的包的作业,如
etrepat/baum
。或
lazychaser/laravel nestedset
。显然,鲍姆已经不复存在了。Sitemenu_模型不是PSR-2类命名,这是一个相当复杂的upo标准,模型部分有点冗余。改为将类命名为SiteMenu。
View => 'create.blade.php'

@foreach($collSiteMenu as $menu)
    @php
            $arrMainMenu[$menu->id] = $menu->menu_name;
        @endphp
@endforeach
@foreach($collSiteMenu as $menu)          
          @isset($menu->id)
            {{ 'menu id is set to '. $menu->id }}
          @endisset
@endforeach
Collection {#380 ▼
  #items: array:12 [▼
    0 => Sitemenu_model {#393 ▼
      #table: "site_navigation"
      #fillable: array:10 [▶]
      +id: null
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:13 [▼
        "id" => 1
        "menu_name" => "About Us"
        "parent_id" => null
        "url_slug" => "about-us"
        "canonical_url" => null
        "show_in_nav" => "show_both"
        "meta_title" => "About Us"
        "meta_keywords" => null
        "meta_description" => "this is test description"
        "is_active" => "yes"
        "display_order" => 1
        "created_at" => "2020-09-02 12:39:20"
        "updated_at" => "2020-09-04 16:21:51"
      ]
      #original: array:13 [▼
        "id" => 1
        "menu_name" => "About Us"
        "parent_id" => null
        "url_slug" => "about-us"
        "canonical_url" => null
        "show_in_nav" => "show_both"
        "meta_title" => "About Us"
        "meta_keywords" => null
        "meta_description" => "this is test description"
        "is_active" => "yes"
        "display_order" => 1
        "created_at" => "2020-09-02 12:39:20"
        "updated_at" => "2020-09-04 16:21:51"
      ]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "getChildren" => Collection {#411 ▼
          #items: array:2 [▼
            0 => Sitemenu_model {#415 ▼
              #table: "site_navigation"
              #fillable: array:10 [▶]
              +id: null
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:13 [▶]
              #original: array:13 [▼
                "id" => 7
                "menu_name" => "Who We Are"
                "parent_id" => 1
                "url_slug" => "who-we-are"
                "canonical_url" => null
                "show_in_nav" => null
                "meta_title" => "Who We Are"
                "meta_keywords" => null
                "meta_description" => null
                "is_active" => "yes"
                "display_order" => 1
                "created_at" => "2020-09-02 14:14:03"
                "updated_at" => "2020-09-02 14:14:03"
              ]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
            1 => Sitemenu_model {#416 ▼
              #table: "site_navigation"
              #fillable: array:10 [▶]
              +id: null
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:13 [▶]
              #original: array:13 [▼
                "id" => 8
                "menu_name" => "Our Cause"
                "parent_id" => 1
                "url_slug" => "our-cause"
                "canonical_url" => null
                "show_in_nav" => null
                "meta_title" => "Our Cause"
                "meta_keywords" => null
                "meta_description" => null
                "is_active" => "yes"
                "display_order" => 2
                "created_at" => "2020-09-02 14:15:27"
                "updated_at" => "2020-09-02 14:15:27"
              ]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }