Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

javediq_143's avatar

Record Id in Collection is Not Set

Hello All,

Today I'm facing some very weird behavior in Laravel while fetching records from the table. I'm having a table which has One to Many Relation with itself. A Menu item can have multiple Submenu. Below is a brief structure:

Table name: site_navigation

id	=> Primary Key, Auto Increment
parent_id	=> Index, Defaults Null
menu_name	=> Varchar
Model => 'Sitemenu_model.php'

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Sitemenu_model extends Model
{
    protected $table = 'site_navigation';

    public function getParent()
    {
        return $this->belongsTo('App\Models\backend\Sitemenu_model', 'parent_id');
    }

    public function getChildren()
    {
        return $this->hasMany('App\Models\backend\Sitemenu_model', 'parent_id');
    }
}
Controller => 'Pagescontent.php'

use App\Models\backend\Sitemenu_model;
class Pagescontent extends Controller
{
	$collSiteMenu = Sitemenu_model::with('getChildren')
            ->orderBy('display_order', 'asc')
            ->get();

	return view('backend.pages.pagescontent.create', ['collSiteMenu'=>$collSiteMenu]);
}
View => 'create.blade.php'

@foreach($collSiteMenu as $menu)
	@php
            $arrMainMenu[$menu->id] = $menu->menu_name;
        @endphp
@endforeach

The '$arrMainMenu' is only having the last record of the collection. When i tried to print $menu->id, it prints blank. Even the child record doesn't print its menu id. Even if i use below code, still no output. Which means 'id' attribute is not set.

@foreach($collSiteMenu as $menu)          
          @isset($menu->id)
            {{ 'menu id is set to '. $menu->id }}
          @endisset
@endforeach

However, in controller, when i dd($collSiteMenu) it prints below:

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 [▶]
    }

I have searched on Google but couldn't find a solution to this. Can any one guide me as where I'm going wrong.

Much Regards,

Javed

0 likes
1 reply
mattsplat's avatar

Your leaving something out from the code you showed. Your id is listed under attributes as it should be but there is another id with a null value already as a property. Did you define this somewhere else?

Please or to participate in this conversation.