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

Shawdow's avatar

How do i write below query to update the login status currently logged in adminusers

$status = \DB::table('admin')->where(['login_status',"no"]);
             
 $status->update(['login_status'=>"yes"]);

//above query is updating status for all the adminusers i need to update only adminusers who are logged in..

0 likes
7 replies
randomstranger's avatar

Well, how do you keep tab of who is logged in and who is not? You could create a column where you could keep the user's last activity timestamp and if current timestamp minus last activity timestamp is less than like 2 minutes (some arbitrary value), you could consider the user logged in. Then, you could change your query to get those users with 2 minutes difference between current timestamp and their last activity timestamp and only update status for them.

Shawdow's avatar

@randomstranger I will do like below

 if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
         

       $userstatus = \DB::table('admins')->where(['login_status',"no"]);
             
          
         $userstatus->update(['login_status'=>"yes"]);
         
                
       return redirect()->intended(route('admin.dashboard'));
         
     }
AddWebContribution's avatar

I assume that your admin table having logged in user id you use Authenticated User for id, with that you can check this like:

// Get the currently authenticated user's ID...
$user_id = Auth::id();

$status = \DB::table('admin')
->where('login_status','no')
->where('user_id', $user_id);
Shawdow's avatar

@Saurabhd

//this my admin table

 Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('phone_no');
            $table->string('email')->unique();
            $table->string('login_status')->default('no');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

Shawdow's avatar

@saurabhd I have created fk relationship in another table can you modify the above query do provide solution.....

AddWebContribution's avatar

Ok, check this:

$user_email = Auth::user()->email;

$status = \DB::table('admin')
->where('login_status','no')
->where('email', $user_email);

If it not work, check $user_email value

Shawdow's avatar

@saurabhd I have tried

Trying to get property of non-object as an error

I am not using the user login system using the custom login system...

Please or to participate in this conversation.