Issue
I'm working on a laravel 5.5 and I'm trying to count the number of users from a table and display the result from a view.
But I'm getting an error
"Undefined Variable: count"
This is the function inside the controller:
public function admin(){
$count = DB::select('select count(*) as total from users');
return view('home',['count' => $count]);
}
This is the code inside the view 'home':
<tr>
<td> Total Users </td>
<td> Total Coaches </td>
<td> {{$count}} </td>
</tr>
Solution
You have given the variable in a string like this ['count => $count'], so it didn't work.
Try the code as below in your controller:
public function admin()
{
$count = DB::select('select count(*) as total from users');
return view('home', ['count' => $count[0]->total]);
}
Answered By - Nandy
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.