【发布时间】:2018-12-09 13:04:55
【问题描述】:
我正在尝试使用多对多关系在我的中间表上获取所有Level_id和Term_id > 但我无法让它工作,我想使用 有序列表 html 将它传递给我的视图。
我总是以这个结尾..试图获得非对象的属性
控制器:
public function get_term_level()
{
$terms=Term::find(1);
foreach ($terms->level as $tm) {
$tm->pivot->Level_id;
}
return view('term_level.index',compact('terms'));
}
Term Model和我的LevelModel
一样public function level(){
return $this->belongsToMany(Level::class, 'term_levels')->withPivot('Term_id', 'Level_id');
}
查看
@foreach ($terms->pivot as $tm )
<ul>
<li>{{ $tm->Term_id }}</li>
{{ $tm->Level_id }}
</ul>@endforeach
【问题讨论】:
-
is
'Term_id', 'Level_id'是数据透视字段吗?我认为这些是期限和级别的关系。你能告诉我们你的数据透视表term_levels吗? -
是的 Term_id 和 Level_id 都是数据透视字段,父项是术语和级别表,在我的数据透视表中,term_level 只有主键和外键,没有别的
标签: php laravel foreach many-to-many laravel-blade