Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

udoyen's avatar

Database Query Error

$loginusers = DB::table('sessions')->select('user_id')->distinct()->where('user_id', '!=', null)->get();               

 // Create array to add logged in users
 $logged = [];

This is my attempt to add the result of this database query to my $logged array.

foreach ($loginusers as $loginuser) {
                     
         # code...
                     
         $usernames =  User::select(
                         
                   "users.first_name", "users.last_name", 
                         
                    DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name")
                         )->where('id', '=', $loginuser)->value('full_name');

                     array_push($logged, $usernames);

         
}

Here is the error I am seeing

Object of class stdClass could not be converted to string (View: /var/www/html/genbrooke/resources/views/admin/users/index.blade.php)

0 likes
7 replies
andonovn's avatar

Why you decided you have an error in your database query? The error is saying you are using an object of class stdClass as a string in your resources/views/admin/users/index.blade.php file.

You can use dd($usernames); right after the query to verify it's running as expected. But most probably it is, check your view file and see where you use a variable as a string. If you need help, copy/paste your resources/views/admin/users/index.blade.php file and we may point you where the error may be.

1 like
udoyen's avatar

Here is the part where the array logged goes:


<li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                        Logged In Users <i class="fa fa-bell fa-fw"></i> <i class="fa fa-caret-down"></i>
                    </a>
                    <ul class="dropdown-menu dropdown-alerts">
                    <div id="userlog">
                          @foreach ($logged as $activity)
                            <li>                          
                                <div style="padding: 8px;">
                                    <i class="fa fa-shield fa-rotate-270"></i> {{$activity}}  
                                    {{--  <span class="pull-right text-muted small">4 minutes ago</span>  --}}
                                  </div>                           
                            </li>
                            <li class="divider"></li>                             
                        @endforeach  
                        </div>
                                             
                    </ul>
                    <!-- /.dropdown-alerts -->
                </li>

Please note I am sending data from a ComposerServiceProvider file!

okaufmann's avatar

As you use

$loginusers = DB::table('sessions')->select('user_id')->distinct()->where('user_id', '!=', null)->get();  

to load all logged in Users and then foreach them with

foreach ($loginusers as $loginuser) {

// ..

The variable $loginuser in the row object (a stdClass).

Then in

->where('id', '=', $loginuser)

It wan't be converted to a string to be compared with id what raises the Exception.

Just use $loginuser->id.

Or what would be much faster, if you pluck all ids make a WHERE IN(1,2,3,4) like query like the following:

$loginusers = DB::table('sessions')->select('user_id')->distinct()->where('user_id', '!=', null)->get();

$userIds = $loginusers->pluck('id');

User::select("users.first_name", "users.last_name", 
                         
                    DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name")
                         )->whereIn('id',$userIds->all())->value('full_name');
2 likes
udoyen's avatar

@okaufmann please note that: $loginusers = DB::table('sessions')->select('user_id')->distinct()->where('user_id', '!=', null)->get() returns just the session user_id that I need to use and compare with the users in the users table so I can pick each one.

udoyen's avatar

@okaufmann I used your code with slight modification to fix it. Thanks for the help.


$loginusers = DB::table('sessions')->select('user_id')
                                ->distinct()->whereNotNull('user_id')
                                ->get();

                $userIds = $loginusers->pluck('user_id');

                // Create array to add logged in users
                $logged = User::select("users.first_name", "users.last_name",
                    DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name")
                )->whereIn('id', $userIds->all())->get();             

Please or to participate in this conversation.