Reputation: 399
I have this line of code in a View::make() method within a controller:
'playersOnline' => count(Session::all())
I have no user created Session class, as I believe one already exists elsewhere and has the function all() in it.
The sessions table has 1 entry, but this line of code returns 3. All I am trying to do is get the total number of rows in this table. How could I change this, to achieve my goal?
Update. I now have this functional code:
$playersOnline = DB::table('sessions') -> select('*') -> count();
This returns the correct data. Now is there any problems with doing this?
Upvotes: 1
Views: 6226
Reputation: 31078
You are mixing different things:
count(Session::all())
The Session
class in Laravel is used to access the PHP session. This class has a function called all()
that will return all data from the session, like you can see it in the documentation. In your code you use the count()
PHP function on the result of this command.
DB::table('sessions')->select('*')->count();
Your second code is a database query made with Laravel's query builder. You select all rows from the sessions
table in the database then you count them with your database management system, for example MySQL.
With the Eloquent ORM you can create smart models. With these you can create instances that will help you access and manipulate the database through them, with the help of Eloquent's methods like all()
that will return all the rows of the table like model instances. Just take a look at the documentation.
Upvotes: 4