2

how can I get & register the logged-in user ID in a table, with October cms

thank you in advance!

1 Answers1

6

You can simply use this Auth facade to get current logged-in user

// =====> forntend
// Returns the signed in user
$user = Auth::getUser();

// now use
// $user->id in your code

// =====> backend
use BackendAuth;
$user = BackendAuth::getUser();

// now use
// $user->id in your code

In back-end if you want to add current logged in BE-User id then

you need to add beforeSave event method to you model in which you want to log be user id also in addition you need to add this log_be_user_id field to your database table

use BackendAuth;

public function beforeSave() {

    // check if we are in backend
    if(App::runningInBackend()) {

        // we assign the be logged in user id 
        $user = BackendAuth::getUser();
        $this->log_be_user_id = $user->id;
    }
}

if any doubts please comment

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • Thank you for your answer but is it possible to do with the builder (RainLab) plugin, if so how? – Lutte P. Okamango Apr 18 '18 at 09:14
  • seems we can not achieve it directly by the `builder (RainLab) plugin`, we need add some code on our own - `you can add field using builder` then create model - after inside that model add this code. then every time user create or update record `log_be_user_id ` will be updated with the `current logged in be user id` automatically – Hardik Satasiya Apr 18 '18 at 12:08
  • thank you so much brother, you really helped me, it works – Lutte P. Okamango Apr 18 '18 at 21:40