Hari Hari - 9 days ago 5
PHP Question

Laravel Undefined property: stdClass::$id error

I am very much new to laravel, and even some minor errors scares me..i am fetching the data from a table with name 'student' but while i do that i am getting an error as


Undefined property: stdClass::$id


it would be great if i get some help here..thank you

StudViewController.php

<?php
namespace App\Http\Controllers;
use DB;
use App\Http\Controllers\Controller;

class StudViewController extends Controller
{
//
public function index(){
$users = DB::select('select * from student');
return view('stud_view',['users'=>$users]);
}
}


stud_view.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<table border = 1>
<tr>
<td>ID</td>
<td>Name</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
</tr>
@endforeach
</table>
</div>
</div>
@endsection


routes.php

Route::get('view-records','StudViewController@index');

Answer

Try this...

@foreach ($users as $user)
 <tr>
    <td>{{ $user->Id }}</td>
    <td>{{ $user->Name }}</td>
 </tr>
 @endforeach
Comments