【问题标题】:Laravel Pivot table get value and pass to blade templateLaravel 数据透视表获取值并传递给刀片模板
【发布时间】:2018-12-09 13:04:55
【问题描述】:

我正在尝试使用多对多关系在我的中间表上获取所有Level_idTerm_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


【解决方案1】:

试试这个(在你的控制器内):

foreach ($terms->level()->get() as $tm) {
    $tm->pivot->Level_id;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多