1

I trying to use Laravel count() function to get the count of row. I have below code to count the number of row of 2 join table.

Example 1:

$row = DB::table('log_user_login')
                ->select(DB::raw('log_user_login.Password as LogPassword'), 'user.*')
                ->join('user', 'log_user_login.Username', '=', 'user.Username')
                ->where('log_user_login.LoginSession', '!=', '')
                ->groupBy('user.ID')
                ->get();
$count = sizeof($row);

Example 2:

$count = DB::table('log_user_login')
            ->select(DB::raw('log_user_login.Password as LogPassword'), 'user.*')
            ->join('user', 'log_user_login.Username', '=', 'user.Username')
            ->where('log_user_login.LoginSession', '!=', '')
            ->groupBy('user.ID')
            ->count();

When I echo $count form Example 1, the number of $count is 15415. But $count of Example 2 return me 89. May I know why this happen and how can I get the number of row without using the get()?

3 Answers 3

3

Example 1 displays the sizeof array in bytes, but example 2 displays total count returned by table.

So, best way to get total count is using ->count() as done in example 2.

Hope you got your answer.

Sign up to request clarification or add additional context in comments.

Comments

1
Please try it.
$row = DB::table('log_user_login')
                ->select(DB::raw('log_user_login.Password as LogPassword'), 'user.*')
                ->join('user', 'log_user_login.Username', '=', 'user.Username')
                ->where('log_user_login.LoginSession', '!=', '')
                ->groupBy('user.ID')
                ->get();
echo $count = count($row);

Comments

0

Do not use sizeof(). So many time its return amount of memory allocated.

Use count() instead of sizeof().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.