octobercmsoctobercms-backendoctober-form-controller

Get & register the logged-in user ID October cms


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

thank you in advance!


Solution

  • 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