未定义 属性:Illuminate\Database\MySqlConnection::$Id

Undefined property: Illuminate\Database\MySqlConnection::$Id

我试图将一个参数从我的数据库传递到我的视图。

控制器

class StatisticsController extends Controller
{
    public function statistic()
    {
        $data = DB::table('statisticforteacher');
        
        return view('/statistics', compact('data'));
    }
}

路线

Route::get('/statistics', 'StatisticsController@statistic');

Blade/View

<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的table,而且每一列的名字也是对的

但是,我仍然得到以下错误。

ErrorException Undefined property: Illuminate\Database\MySqlConnection::$Id

$data =DB::table('statisticforteacher');

这里 $data return query builder 实例所以你必须 return 收集以便循环数据所以你必须使用

 $data =DB::table('statisticforteacher')->get();

 $data =DB::table('statisticforteacher')->all();

如果你有附加查询条件那么你使用的是get()而不是all()方法

试试这个

$data =DB::table('statisticforteacher')->get();