PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 29, 2021

[FIXED] How to count the number of users from a table through views in laravel?

 December 29, 2021     count, database, laravel-5.5, mysql     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing