我有 3 张桌子;一个用于课程,一个用于课程表,另一个用于类别表,其中类别可以有多个课程。我想显示带有 classe_name 和 category_name 的课程列表。我在还包含课程名称的类和类别之间制作数据透视表,但我无法从表类别中显示 catgory_name 我不知道这是否是正确的方法
//显示课程的方法
public function index()
{
$categories=Course_category::all();
return view('teacher.courses')->with('categories',$categories);
}
刀
@foreach ($categories->classe as $item)
{{$item->category_name}}
@endforeach
类别模型
protected $table = 'categories';
protected $fillable =['id','category_name','categorie_image'];
/**
* The roles that belong to the Course_category
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function classe(){
return $this -> belongsToMany('App\Classe','classe_categorie','categorie_id','classe_id');
}
类模型
protected $table = 'classes';
protected $fillable=['id','classe_name','classe_image'];
public function course_categorie() {
return $this -> belongsToMany('App\Course_category','classe_categorie','classe_id','categorie_id');
}