我如何只为在课程中注册的学生展示课程,并且我有2个桌子和数据透视表?
表1: Home_Students :home_id,home_studid ....
表2: Hw_Classes :class_id,class_desc,class_date ....
数据透视表: Hw_StudentClasses :Stclass_id,Stclass_classid,Stclass_studid
所以我创建了一个模型 MySeminarClasses 来与表 Hw_StudentClasses 和一个Controller MySeminarClassesController
进行通信我在其他表belongsToMany
的模型中建立了关系public function Users(){
return $this->belongsToMany('User','home_id');
}
public function SeminarClass(){
return $this->belongsToMany('SeminarClass','class_id');
}
同样在控制器中我做了这个,我不确定它是否正确,但我是从laravel 4.2文档的说明中做到的
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->orderBy('class_date',strtotime('-4 month'))
->get();
最后在刀片中
<tbody>
@foreach($myclasses as $i=>$myclass)
<tr>
<td>{{ $i+1 }}</td>
**<td>{{ link_to_route('class.show',$myclass->Class_desc,$myclass->Class_id) }}</td>**
<td class="text-center">{{ date('j-n-Y G:i',strtotime($myclass->class_date)) }}</td>
<td class="text-center">{{ UserEnroll::where('Stclass_classid',$myclass->Class_id)->count() }}</td>
</tr>
@endforeach
</tbody>
答案 0 :(得分:1)
经过16个小时的奋斗......我回答了自己的问题!这个使用 Auth :: user 的查询帮助了我。仅为每个登录的用户显示类
$id = Auth::user()->home_studid;
$date = date('m-d-Y', strtotime('-4 month'));
$seminars = Seminar::where('Package_Display', 1)
->orWhere('Package_Display', 2)
->orderBy('Package_Name', 'asc')
->get();
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->where('Home_Students.home_studid','=',$id)
->orderBy('class_date',strtotime('-4 month'))
->get();
希望这有助于某人! ^ _ ^