Wednesday, October 19, 2022

[FIXED] How to show number of registered users in Laravel based on usertype?

Issue

i'm trying to display data from the database in the admin dashboard

i used this:

            <?php
            use Illuminate\Support\Facades\DB;
            $users = DB::table('users')->count();
            echo $users;
            ?>

and i have successfully get the correct data from the database

but what if i want to display a specific data

for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype

i tried this:

<?php
            use Illuminate\Support\Facades\DB;
            $users = DB::table('users')->count()->WHERE usertype =admin;
            echo $users;
            ?>

but it didn't work, what am i doing wrong?


Solution

you have the wrong syntax. Please read docs carefully...Btw here is the code that you can try

use Illuminate\Support\Facades\DB;

        $users = DB::table('users')->where('usertype','admin')->count();
        echo $users;
        ?>

and for working in the laravel see the usage of Model which will be better for you..



Answered By - Tanvir Ahmed
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

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