Reputation: 23
StaffController
public function show($id){
$staffinfo = DB::table('staff')->where('user_id', $id)->get();
return view('staff.view')->with('staffinfo', $staffinfo);
}
view.blade.php
<h1>{{$staffinfo->name}}</h1>
<p>{{$staffinfo->user_id}}</p>
Is this code right to show data from staff table in view by show($id)
function?
Getting error:
"Property [name] does not exist on this collection instance. (View: F:\xampp\htdocs\gchsc\resources\views\staff\view.blade.php)"
Upvotes: 0
Views: 1088
Reputation: 29278
Switch ->get()
to ->first()
. $staffInfo
is a Collection
of database records, not a single one:
StaffController.php
$staffinfo = DB::table('staff')->where('user_id', $id)->first();
Then the following will work in your view:
staff/view.blade.php
<h1>{{ $staffinfo->name }}</h1>
<p>{{ $staffinfo->user_id }}</p>
Or, leave your code as is and iterate in your view:
StaffController.php
$staffinfo = DB::table('staff')->where('user_id', $id)->get();
staff/view.blade.php
@foreach($staffInfo AS $staff){
<h1>{{ $staff->name }}</h1>
<p>{{ $staff->user_id }}</p>
@endforeach
Upvotes: 2