Skip to content
Advertisement

Undefined property: IlluminateDatabaseMySqlConnection::$Id

I was trying to pass a parameter from my database to my view.

Controller

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

Route

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>

And from PHPMyAdmin, we can see a table named statisticforteacher, and each column name is also right.

enter image description here

However, I still get the following error.

ErrorException Undefined property: IlluminateDatabaseMySqlConnection::$Id

Advertisement

Answer

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

Here $data return query builder instance so you have to return collection in order to loop data so you have to use

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

or

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

if you have addition query condition then you have use get() not all() method

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement