Documentation

User extends AbstractBaseApi
in package

User Class

Represents a user in the Canvas LMS. This class provides methods to create, update, and find users from the Canvas LMS system. It utilizes Data Transfer Objects (DTOs) for handling user creation and updates.

Usage Examples:

// Creating a new user
$userData = [
    'username' => 'john_doe',
    'email' => 'john.doe@example.com',
    // ... other user data ...
];
$user = User::create($userData);

// Updating an existing user
$updatedData = [
    'email' => 'new_john.doe@example.com',
    // ... other updated data ...
];
$updatedUser = User::update(123, $updatedData); // where 123 is the user ID

// Finding a user by ID
$user = User::find(123);

// Fetching all users (first page only)
$users = User::fetchAll();

// Fetching users with pagination support
$paginatedResponse = User::fetchAllPaginated(['per_page' => 10]);
$users = $paginatedResponse->getJsonData();
$pagination = $paginatedResponse->toPaginationResult($users);

// Fetching a specific page of users
$paginationResult = User::fetchPage(['page' => 2, 'per_page' => 10]);
$users = $paginationResult->getData();
$hasNext = $paginationResult->hasNext();

// Fetching all users from all pages
$allUsers = User::fetchAllPages(['per_page' => 50]);

Table of Contents

Properties

$avatarState  : string|null
Optional: If avatars are enabled and caller is admin, this field can be requested and will contain the current state of the user's avatar.
$avatarUrl  : string|null
If avatars are enabled, this field will be included and contain a url to retrieve the user's avatar.
$bio  : string|null
Optional: The user's bio.
$email  : string|null
Optional: This field can be requested with certain API calls, and will return the users primary email address.
$enrollments  : array<string|int, mixed>|null
Optional: This field can be requested with certain API calls, and will return a list of the users active enrollments. See the List enrollments API for more details about the format of these records.
$firstName  : string
The first name of the user.
$id  : int
The ID of the user.
$integrationId  : string|null
The integration_id associated with the user. This field is only included if the user came from a SIS import and has permissions to view SIS information.
$lastLogin  : string|null
Optional: This field is only returned in certain API calls, and will return a timestamp representing the last time the user logged in to canvas.
$lastName  : string
The last name of the user.
$locale  : string|null
Optional: This field can be requested with certain API calls, and will return the users locale in RFC 5646 format.
$loginId  : string
The unique login id for the user. This is what the user uses to log in to Canvas.
$name  : string
The name of the user.
$shortName  : string
A short name the user has selected, for use in conversations or other less formal places through the site.
$sisImportId  : int|null
The id of the SIS import. This field is only included if the user came from a SIS import and has permissions to manage SIS information.
$sisUserId  : string|null
The SIS ID associated with the user. This field is only included if the user came from a SIS import and has permissions to view SIS information.
$sortableName  : string
The name of the user that it should be used for sorting groups of users, such as in the gradebook.
$timeZone  : string|null
Optional: This field is only returned in certain API calls, and will return the IANA time zone name of the user's preferred timezone.
$apiClient  : HttpClientInterface
$methodAliases  : array<string|int, mixed>
Define method aliases

Methods

__callStatic()  : mixed
Magic method to handle function aliases
__construct()  : mixed
BaseApi constructor.
create()  : self
Create a new User instance.
enrollments()  : array<string|int, Enrollment>
Get enrollments for this user (relationship method alias with parameter support)
fetchAll()  : array<string|int, User>
Fetch all users
fetchAllPages()  : array<string|int, User>
Fetch all users from all pages
fetchAllPaginated()  : PaginatedResponse
Fetch users with pagination support
fetchPage()  : PaginationResult
Fetch users from a specific page
find()  : self
Find a single user by ID.
getActiveEnrollments()  : array<string|int, Enrollment>
Get active enrollments for this user
getAvatarState()  : string|null
getAvatarUrl()  : string|null
getBio()  : string|null
getEmail()  : string|null
getEnrollments()  : array<string|int, mixed>|null
getEnrollmentsAsObjects()  : array<string|int, Enrollment>
Get all enrollments for this user as Enrollment objects
getEnrollmentsData()  : array<string|int, mixed>|null
Get the user's enrollment data array (legacy method - uses embedded data)
getEnrollmentsInCourse()  : array<string|int, Enrollment>
Get enrollments for this user in a specific course
getFirstName()  : string
getId()  : int
getIntegrationId()  : string|null
getLastLogin()  : string|null
getLastName()  : string
getLocale()  : string|null
getLoginId()  : string
getName()  : string
getShortName()  : string
getSisImportId()  : int|null
getSisUserId()  : string|null
getSortableName()  : string
getStudentEnrollments()  : array<string|int, Enrollment>
Get student enrollments for this user
getTeacherEnrollments()  : array<string|int, Enrollment>
Get teacher enrollments for this user
getTimeZone()  : string|null
isEnrolledInCourse()  : bool
Check if this user is enrolled in a specific course
isStudentInCourse()  : bool
Check if this user is a student in a specific course
isTeacherInCourse()  : bool
Check if this user is a teacher in a specific course
mergeInto()  : bool
Merge current user into another one
save()  : bool
Save the user to the Canvas LMS.
setApiClient()  : void
Set the API client
setAvatarState()  : void
setAvatarUrl()  : void
setBio()  : void
setEmail()  : void
setEnrollments()  : void
setFirstName()  : void
setId()  : void
setIntegrationId()  : void
setLastLogin()  : void
setLastName()  : void
setLocale()  : void
setLoginId()  : void
setName()  : void
setShortName()  : void
setSisImportId()  : void
setSisUserId()  : void
setSortableName()  : void
setTimeZone()  : void
update()  : self
Update an existing user.
castValue()  : DateTime|mixed
Cast a value to the correct type
checkApiClient()  : void
Check if the API client is set, if not, instantiate a new one
convertPaginatedResponseToModels()  : array<string|int, static>
Helper method to convert paginated response data to model instances
createPaginationResult()  : PaginationResult
Helper method to create PaginationResult from paginated response
fetchAllPagesAsModels()  : array<string|int, static>
Helper method to fetch all pages and convert to model instances
getPaginatedResponse()  : PaginatedResponse
Helper method to get paginated response from API endpoint
populate()  : void
Populate the object with new data
toDtoArray()  : array<string|int, mixed>
Convert the object to an array
createFromDTO()  : self
Create a User from a CreateUserDTO.
updateFromDTO()  : self
Update a user from a UpdateUserDTO.

Properties

$avatarState

Optional: If avatars are enabled and caller is admin, this field can be requested and will contain the current state of the user's avatar.

public string|null $avatarState

$avatarUrl

If avatars are enabled, this field will be included and contain a url to retrieve the user's avatar.

public string|null $avatarUrl

$bio

Optional: The user's bio.

public string|null $bio

$email

Optional: This field can be requested with certain API calls, and will return the users primary email address.

public string|null $email

$enrollments

Optional: This field can be requested with certain API calls, and will return a list of the users active enrollments. See the List enrollments API for more details about the format of these records.

public array<string|int, mixed>|null $enrollments

$firstName

The first name of the user.

public string $firstName

$id

The ID of the user.

public int $id

$integrationId

The integration_id associated with the user. This field is only included if the user came from a SIS import and has permissions to view SIS information.

public string|null $integrationId

$lastLogin

Optional: This field is only returned in certain API calls, and will return a timestamp representing the last time the user logged in to canvas.

public string|null $lastLogin

$lastName

The last name of the user.

public string $lastName

$locale

Optional: This field can be requested with certain API calls, and will return the users locale in RFC 5646 format.

public string|null $locale

$loginId

The unique login id for the user. This is what the user uses to log in to Canvas.

public string $loginId

$name

The name of the user.

public string $name

$shortName

A short name the user has selected, for use in conversations or other less formal places through the site.

public string $shortName

$sisImportId

The id of the SIS import. This field is only included if the user came from a SIS import and has permissions to manage SIS information.

public int|null $sisImportId

$sisUserId

The SIS ID associated with the user. This field is only included if the user came from a SIS import and has permissions to view SIS information.

public string|null $sisUserId

$sortableName

The name of the user that it should be used for sorting groups of users, such as in the gradebook.

public string $sortableName

$timeZone

Optional: This field is only returned in certain API calls, and will return the IANA time zone name of the user's preferred timezone.

public string|null $timeZone

$methodAliases

Define method aliases

protected static array<string|int, mixed> $methodAliases = ['fetchAll' => ['all', 'get', 'getAll'], 'find' => ['one', 'getOne'], 'fetchAllPaginated' => ['allPaginated', 'getPaginated'], 'fetchAllPages' => ['allPages', 'getPages'], 'fetchPage' => ['page', 'getPage']]

Methods

__callStatic()

Magic method to handle function aliases

public static __callStatic(string $name, array<string|int, mixed> $arguments) : mixed
Parameters
$name : string
$arguments : array<string|int, mixed>

__construct()

BaseApi constructor.

public __construct(array<string|int, mixed> $data) : mixed
Parameters
$data : array<string|int, mixed>

create()

Create a new User instance.

public static create(array<string|int, mixed>|CreateUserDTO $userData) : self
Parameters
$userData : array<string|int, mixed>|CreateUserDTO
Tags
throws
Exception
Return values
self

enrollments()

Get enrollments for this user (relationship method alias with parameter support)

public enrollments([array<string|int, mixed> $params = [] ]) : array<string|int, Enrollment>

This method provides a clean, explicit way to access user enrollments across all courses without conflicts with Canvas API data structure. Supports all Canvas API enrollment parameters for filtering.

Parameters
$params : array<string|int, mixed> = []

Query parameters for filtering enrollments:

  • type[]: Filter by enrollment type (e.g., ['StudentEnrollment', 'TeacherEnrollment'])
  • role[]: Filter by enrollment role
  • state[]: Filter by enrollment state (e.g., ['active', 'invited', 'completed'])
  • include[]: Include additional data (e.g., ['course', 'avatar_url'])
Tags
example
$user = User::find(456);

// Get all enrollments for this user
$enrollments = $user->enrollments();

// Get active enrollments only
$activeEnrollments = $user->enrollments([
    'state[]' => ['active']
]);

// Get student enrollments only
$studentEnrollments = $user->enrollments([
    'type[]' => ['StudentEnrollment']
]);

// Get enrollments with course data included
$enrollmentsWithCourses = $user->enrollments([
    'include[]' => ['course']
]);
throws
CanvasApiException

If the user ID is not set or API request fails

Return values
array<string|int, Enrollment>

Array of Enrollment objects

fetchAll()

Fetch all users

public static fetchAll([array<string|int, mixed> $params = [] ]) : array<string|int, User>
Parameters
$params : array<string|int, mixed> = []
Tags
throws
CanvasApiException
Return values
array<string|int, User>

fetchAllPages()

Fetch all users from all pages

public static fetchAllPages([array<string|int, mixed> $params = [] ]) : array<string|int, User>
Parameters
$params : array<string|int, mixed> = []

Query parameters for the request

Tags
throws
CanvasApiException
Return values
array<string|int, User>

fetchAllPaginated()

Fetch users with pagination support

public static fetchAllPaginated([array<string|int, mixed> $params = [] ]) : PaginatedResponse
Parameters
$params : array<string|int, mixed> = []

Query parameters for the request

Tags
throws
CanvasApiException
Return values
PaginatedResponse

fetchPage()

Fetch users from a specific page

public static fetchPage([array<string|int, mixed> $params = [] ]) : PaginationResult
Parameters
$params : array<string|int, mixed> = []

Query parameters for the request

Tags
throws
CanvasApiException
Return values
PaginationResult

find()

Find a single user by ID.

public static find(int $id) : self
Parameters
$id : int
Tags
throws
CanvasApiException
Return values
self

getActiveEnrollments()

Get active enrollments for this user

public getActiveEnrollments([array<string|int, mixed> $params = [] ]) : array<string|int, Enrollment>

Convenience method to get only active enrollments across all courses.

Parameters
$params : array<string|int, mixed> = []

Additional query parameters

Tags
throws
CanvasApiException

If user ID is not set or API request fails

Return values
array<string|int, Enrollment>

Array of active Enrollment objects

getAvatarState()

public getAvatarState() : string|null
Return values
string|null

getAvatarUrl()

public getAvatarUrl() : string|null
Return values
string|null

getBio()

public getBio() : string|null
Return values
string|null

getEmail()

public getEmail() : string|null
Return values
string|null

getEnrollments()

public getEnrollments() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

getEnrollmentsAsObjects()

Get all enrollments for this user as Enrollment objects

public getEnrollmentsAsObjects([array<string|int, mixed> $params = [] ]) : array<string|int, Enrollment>

This method fetches enrollments across all courses for the current user. Use the $params array to filter enrollments by type, state, or other Canvas API parameters.

Parameters
$params : array<string|int, mixed> = []

Query parameters for filtering enrollments (e.g., ['type[]' => ['StudentEnrollment']])

Tags
throws
CanvasApiException

If the user ID is not set or API request fails

Return values
array<string|int, Enrollment>

Array of Enrollment objects

getEnrollmentsData()

Get the user's enrollment data array (legacy method - uses embedded data)

public getEnrollmentsData() : array<string|int, mixed>|null

This returns the raw enrollments array that may be embedded in the user object from certain Canvas API calls. For fetching current enrollments from the API, use getEnrollmentsAsObjects() instead.

Return values
array<string|int, mixed>|null

Raw enrollments data array or null

getEnrollmentsInCourse()

Get enrollments for this user in a specific course

public getEnrollmentsInCourse(int $courseId[, array<string|int, mixed> $params = [] ]) : array<string|int, Enrollment>

Returns only the enrollments for this user in the specified course. This is useful for checking a user's role(s) in a particular course.

Parameters
$courseId : int

The course ID to filter enrollments by

$params : array<string|int, mixed> = []

Additional query parameters

Tags
throws
CanvasApiException

If user ID is not set or API request fails

Return values
array<string|int, Enrollment>

Array of Enrollment objects for the specified course

getFirstName()

public getFirstName() : string
Return values
string

getId()

public getId() : int
Return values
int

getIntegrationId()

public getIntegrationId() : string|null
Return values
string|null

getLastLogin()

public getLastLogin() : string|null
Return values
string|null

getLastName()

public getLastName() : string
Return values
string

getLocale()

public getLocale() : string|null
Return values
string|null

getLoginId()

public getLoginId() : string
Return values
string

getName()

public getName() : string
Return values
string

getShortName()

public getShortName() : string
Return values
string

getSisImportId()

public getSisImportId() : int|null
Return values
int|null

getSisUserId()

public getSisUserId() : string|null
Return values
string|null

getSortableName()

public getSortableName() : string
Return values
string

getStudentEnrollments()

Get student enrollments for this user

public getStudentEnrollments([array<string|int, mixed> $params = [] ]) : array<string|int, Enrollment>

Convenience method to get only student enrollments across all courses.

Parameters
$params : array<string|int, mixed> = []

Additional query parameters

Tags
throws
CanvasApiException

If user ID is not set or API request fails

Return values
array<string|int, Enrollment>

Array of student Enrollment objects

getTeacherEnrollments()

Get teacher enrollments for this user

public getTeacherEnrollments([array<string|int, mixed> $params = [] ]) : array<string|int, Enrollment>

Convenience method to get only teacher enrollments across all courses.

Parameters
$params : array<string|int, mixed> = []

Additional query parameters

Tags
throws
CanvasApiException

If user ID is not set or API request fails

Return values
array<string|int, Enrollment>

Array of teacher Enrollment objects

getTimeZone()

public getTimeZone() : string|null
Return values
string|null

isEnrolledInCourse()

Check if this user is enrolled in a specific course

public isEnrolledInCourse(int $courseId[, string|null $enrollmentType = null ]) : bool
Parameters
$courseId : int

The course ID to check enrollment in

$enrollmentType : string|null = null

Optional: specific enrollment type to check for

Tags
throws
CanvasApiException

If user ID is not set or API request fails

Return values
bool

True if user is enrolled in the course (with optional type filter)

isStudentInCourse()

Check if this user is a student in a specific course

public isStudentInCourse(int $courseId) : bool
Parameters
$courseId : int

The course ID to check

Tags
throws
CanvasApiException

If user ID is not set or API request fails

Return values
bool

True if user has a student enrollment in the course

isTeacherInCourse()

Check if this user is a teacher in a specific course

public isTeacherInCourse(int $courseId) : bool
Parameters
$courseId : int

The course ID to check

Tags
throws
CanvasApiException

If user ID is not set or API request fails

Return values
bool

True if user has a teacher enrollment in the course

mergeInto()

Merge current user into another one

public mergeInto(int $destinationUserId) : bool
Parameters
$destinationUserId : int

The ID of the user to merge into

Tags
throws
Exception
Return values
bool

save()

Save the user to the Canvas LMS.

public save() : bool
Tags
throws
Exception
Return values
bool

setAvatarState()

public setAvatarState(string|null $avatarState) : void
Parameters
$avatarState : string|null

setAvatarUrl()

public setAvatarUrl(string|null $avatarUrl) : void
Parameters
$avatarUrl : string|null

setBio()

public setBio(string|null $bio) : void
Parameters
$bio : string|null

setEmail()

public setEmail(string|null $email) : void
Parameters
$email : string|null

setEnrollments()

public setEnrollments(array<string|int, mixed>|null $enrollments) : void
Parameters
$enrollments : array<string|int, mixed>|null

setFirstName()

public setFirstName(string $firstName) : void
Parameters
$firstName : string

setId()

public setId(int $id) : void
Parameters
$id : int

setIntegrationId()

public setIntegrationId(string|null $integrationId) : void
Parameters
$integrationId : string|null

setLastLogin()

public setLastLogin(string|null $lastLogin) : void
Parameters
$lastLogin : string|null

setLastName()

public setLastName(string $lastName) : void
Parameters
$lastName : string

setLocale()

public setLocale(string|null $locale) : void
Parameters
$locale : string|null

setLoginId()

public setLoginId(string $loginId) : void
Parameters
$loginId : string

setName()

public setName(string $name) : void
Parameters
$name : string

setShortName()

public setShortName(string $shortName) : void
Parameters
$shortName : string

setSisImportId()

public setSisImportId(int|null $sisImportId) : void
Parameters
$sisImportId : int|null

setSisUserId()

public setSisUserId(string|null $sisUserId) : void
Parameters
$sisUserId : string|null

setSortableName()

public setSortableName(string $sortableName) : void
Parameters
$sortableName : string

setTimeZone()

public setTimeZone(string|null $timeZone) : void
Parameters
$timeZone : string|null

update()

Update an existing user.

public static update(int $id, UpdateUserDTO|array<string|int, mixed> $userData) : self
Parameters
$id : int
$userData : UpdateUserDTO|array<string|int, mixed>
Tags
throws
CanvasApiException
throws
Exception
Return values
self

castValue()

Cast a value to the correct type

protected castValue(string $key, mixed $value) : DateTime|mixed
Parameters
$key : string
$value : mixed
Tags
throws
Exception
Return values
DateTime|mixed

checkApiClient()

Check if the API client is set, if not, instantiate a new one

protected static checkApiClient() : void

convertPaginatedResponseToModels()

Helper method to convert paginated response data to model instances

protected static convertPaginatedResponseToModels(PaginatedResponse $paginatedResponse) : array<string|int, static>
Parameters
$paginatedResponse : PaginatedResponse
Return values
array<string|int, static>

fetchAllPagesAsModels()

Helper method to fetch all pages and convert to model instances

protected static fetchAllPagesAsModels(string $endpoint[, array<string|int, mixed> $params = [] ]) : array<string|int, static>
Parameters
$endpoint : string

The API endpoint path

$params : array<string|int, mixed> = []

Query parameters for the request

Return values
array<string|int, static>

getPaginatedResponse()

Helper method to get paginated response from API endpoint

protected static getPaginatedResponse(string $endpoint[, array<string|int, mixed> $params = [] ]) : PaginatedResponse
Parameters
$endpoint : string

The API endpoint path

$params : array<string|int, mixed> = []

Query parameters for the request

Return values
PaginatedResponse

populate()

Populate the object with new data

protected populate(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>
Tags
throws
Exception

toDtoArray()

Convert the object to an array

protected toDtoArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

        
On this page

Search results