问题描述
我试图将参数从我的数据库传递到我的视图。
控制器
class StatisticsController extends Controller
{
public function statistic()
{
$data = DB::table('statisticforteacher');
return view('/statistics',compact('data'));
}
}
路线
Route::get('/statistics','StatisticsController@statistic');
刀片/视图
<table class="table-responsive"
style="border-collapse: separate; border-spacing: 10px 15px;">
<thead>
<th>Id</th>
<th>Kapitel</th>
<th>Frage</th>
<th>Anzahl der richtige Antwort</th>
<th>Anzahl der falsche Antwort</th>
<th>Richtige Rate</th>
</thead>
<tbody>
@foreach($data as $value)
<tr>
<td>{{$value -> Id}}</td>
<td>{{$value -> FrageBezeichnung}}</td>
<td>{{$value -> Kapitel}}</td>
<td>{{$value -> richtigeAntwort}}</td>
<td>{{$value -> falscheAntwort}}</td>
<td>{{$value -> richtigeRate}}</td>
</tr>
@endforeach
</tbody>
</table>
而从PHPMyAdmin中,我们可以看到一个名为statisticforteacher
的表,每个列名也是对的。
但是,我仍然收到以下错误。
ErrorException 未定义的属性: Illuminate\Database\MysqLConnection::$Id
解决方法
$data =DB::table('statisticforteacher');
这里 $data
返回 query builder
实例所以你必须返回集合才能循环数据所以你必须使用
$data =DB::table('statisticforteacher')->get();
或
$data =DB::table('statisticforteacher')->all();
如果您有附加查询条件,那么您使用的是 get()
而不是 all()
方法
试试这个
$data =DB::table('statisticforteacher')->get();