To retrieve the UserID of a user, we first use the User
facade from Statamic. With User::query()->where('name', 'Sebastian Widmann')->first();
, the user with the name 'Sebastian Widmann' is queried. first()
ensures that only the first matching entry is returned. Subsequently, the id
of the found user is stored in the $user_id
variable.
use Statamic\Facades\User;
$user = User::query()->where('name', 'Sebastian Widmann')->first();
$user_id = $user->id;
To find a user, there are different methods in Statamic. The User
facade provides methods to find a user by email, ID or name. Please note that the name search does not necessarily yield unique results if there are multiple users with the same name.
use Statamic\Facades\User;
// Query user by email
$user = User::findByEmail('user@example.com');
// Query user by ID
$user = User::find('f4198f3a-cb67-4fa4-b69a-834fb4970002');
// Query user by name (Note, not necessarily unique)
$user = User::query()->where('name', 'Sebastian Widmann')->first();
To find all superusers, we use the User
facade and run a query where we set the 'super' attribute to 'true'. get()
is used to retrieve all users that meet the condition.
use Statamic\Facades\User;
User::query()->where('super', true)->get();
The User
facade also offers a method to determine the currently logged-in user. User::current();
returns the user object of the currently logged-in user.
use Statamic\Facades\User;
User::current();