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

前端 未结 2 1290
后悔当初
后悔当初 2020-12-22 04:24

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条回答
  •  独厮守ぢ
    2020-12-22 05:16

    Look at the relation - it is hasMany relation. meaning, on project has many project items.

    try thhis,

     @foreach ($projects as $project) 
        @foreach ($project->projectitem as $projectitem)
           echo $projectitem->id
        @endforeach
     @endforeach
    

    NOTE: this inner loop is necessary only if the relation is hasMany. if it is hasOne, no need to change anything in you loop, just change the relation to hasOne and run.

提交回复
热议问题