Displaying admin username in October CMS backend form

Hey everyone! I’m working on an October CMS project and I’m currently facing a challenge. I need to display the logged-in admin’s username in a backend form field but I’m unable to retrieve it using a partial. I’ve set up everything, yet the field remains blank. Could someone guide me on how to correctly fetch the current admin’s name?

Below is an example of what I’m trying to accomplish:

<?php
// In my partial
$currentUserName = // Insert code here to fetch the current admin's username
?>

<div class="form-group">
    <label>Admin Name:</label>
    <input type="text" value="<?= $currentUserName ?>" readonly>
</div>

I appreciate any help you can offer!

hey there creativechef89! have u tried using the BackendAuth facade? it’s super handy for getting admin info.

something like this might work:

$username = BackendAuth::getUser()->login;

curious tho, why do u need to show the admin name? any specific reason? sounds like an interesting project!

hey creativechef89, i ran into this before. try using the Auth::getUser() method to grab the current user. then you can access their username like this:

$user = Auth::getUser();
$username = $user ? $user->login : ‘not logged in’;

stick that in ur partial and it should work. lmk if u need more help!

I’ve encountered a similar issue in one of my October CMS projects. The solution is to use the BackendAuth facade to access the currently logged-in user’s information. Here’s how you can modify your partial to display the admin’s username:

<?php
use Backend\Facades\BackendAuth;

$currentUser = BackendAuth::getUser();
$currentUserName = $currentUser ? $currentUser->first_name . ' ' . $currentUser->last_name : 'Not logged in';
?>

<div class="form-group">
    <label>Admin Name:</label>
    <input type="text" value="<?= e($currentUserName) ?>" readonly>
</div>

This approach ensures you’re fetching the correct user data from the backend authentication system. Remember to use the ‘e()’ function for proper escaping of the output. Hope this helps solve your problem!