I was trying to pass a parameter from my database to my view.
Controller
JavaScript
x
class StatisticsController extends Controller
{
public function statistic()
{
$data = DB::table('statisticforteacher');
return view('/statistics', compact('data'));
}
}
Route
JavaScript
Route::get('/statistics', 'StatisticsController@statistic');
Blade/View
JavaScript
<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.
However, I still get the following error.
ErrorException Undefined property: IlluminateDatabaseMySqlConnection::$Id
Advertisement
Answer
JavaScript
$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
JavaScript
$data =DB::table('statisticforteacher')->get();
or
JavaScript
$data =DB::table('statisticforteacher')->all();
if you have addition query condition then you have use get()
not all()
method