错误:尝试获取非对象的属性'gender_id'
控制器:
public function printreports(Request $request)
{
$id = $request->get('select2'); //eg. id=1
$teachers = DB::table('teachers')->find($id);
return view('teachers.report1',compact('teachers'));
}
查看:
@foreach($teachers as $teacher)
{{$teacher->gender_id}}
@endforeach
如果我用Eloquent通过替换以下查询来做到这一点,那还是可以的,但是我想如上所述通过数据库查询来做到这一点。
$teachers = Teacher::find($id);
答案 0 :(得分:1)
您需要在何处使用它,之后您将可以进行收藏。
$teachers = DB::table('teachers')->where('id', $id)->get();
使用查找时,您只有一个物品
答案 1 :(得分:0)
使用foreach
时不需要使用find()
。
@if(sizeof($teachers))
{{$teachers->gender_id}}
{{$teachers->gender_id}}//whatever field you need to display
{{$teachers->gender_id}}//whatever field you need to display
@endif
如果您使用get()
,
$teachers = DB::table('teachers')->where('id','=',$id)->get();
@if(sizeof($teachers))
@foreach($teachers as $teacher)
{{$teacher->gender_id}}
{{$teacher->gender_id}}//whatever field you need to display
{{$teacher->gender_id}}//whatever field you need to display
@endforeach
@endif
答案 2 :(得分:0)
$teachers = DB::table('teachers')->find($id);
将返回单个模型或null
,请尝试使用$teachers = DB::table('teachers')->get();
答案 3 :(得分:0)
如果您想返回集合以查看,请使用get()
这样的方法:-
public function printreports(Request $request)
{
$id = $request->get('select2');
$teachers = DB::table('teachers')->where('id', $id)->get();
return view('teachers.report1',compact('teachers'));
}