Home > Software design >  Count from database and show output
Count from database and show output

Time:12-06

I want to count the user IDs in the database and show the total count as output, but I get an error. I use the following code:

//Controller Code

public function nsure(){
$users = DB::table('users')->count();
return view('nsure',compact(['users']));
}

//blade view code

@foreach ($users as $user)
    <span class="" style="color:black;  font-size: 14px">{{$user->id }}</span>
@endforeach

CodePudding user response:

To show all user IDs:

Controller

$users = DB::table('users')->get();

Blade

@foreach ($users as $user)
    <span class="" style="color:black;  font-size: 14px">{{$user->id}}</span>
@endforeach

To show the total count of all users

Controller

$userCount = DB::table('users')->count();

Blade

<span class="" style="color:black;  font-size: 14px">{{$userCount}}</span>

CodePudding user response:

$users = DB::table('users')->count();, $users is an integer, so you can not iterate it.

  • Related