Laravel HasMany relationship Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$id

前端 未结 2 1206
孤独总比滥情好
孤独总比滥情好 2020-12-22 04:29

i have a problem with reading out data with a relationship. some how it returns Undefined property: Illuminate\\Database\\Eloquent\\Relations\\HasMany::$id. I have no clue w

2条回答
  •  -上瘾入骨i
    2020-12-22 05:04

    You have collection of Projects Object in the $projects after you perform a loop you get each project as $project depending on the state of the loop. When you call the projectitem It will return a collection of ProjectItem and not a single ProjectItem that is the reason why $id is undefined. You should form another loop to get each ProjectItem separatly

    @foreach ($projects as $project) 
    
        // $project->projectitem() return a collection of projectitem 
        // and not a single projectitem
        // to access ID of each projectitem you must perform another loop
    
        @foreach ($project->projectitem() as $projectitem)
             // here project id is disponible as property accessible like this
             {{ $projectitem->id }}
        @endforeach
    @endforeach
    

提交回复
热议问题