【问题标题】:Undefined property: Illuminate\Database\MySqlConnection::$Id未定义的属性:Illuminate\Database\MySqlConnection::$Id
【发布时间】:2021-09-15 13:48:44
【问题描述】:

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

控制器

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 未定义属性: 照亮\数据库\MySqlConnection::$Id

【问题讨论】:

    标签: php laravel laravel-blade


    【解决方案1】:

    试试这个

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

    【讨论】:

      【解决方案2】:
      $data =DB::table('statisticforteacher');
      

      这里$data返回query builder实例所以你必须返回集合才能循环数据所以你必须使用

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

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

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

      【讨论】:

      • 是的,对,我怎么会犯这么愚蠢的错误:)
      猜你喜欢
      • 2018-11-18
      • 2019-10-18
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 2014-11-23
      • 2016-06-02
      • 2016-09-05
      • 2017-02-12
      相关资源
      最近更新 更多