DashboardService
Table of contents
1. Overview Table of Contents
The DashboardService class provides helper functions for administrative dashboard operations, including access restrictions and the searchable, filterable, paginated user list. It ensures that the logged-in admin user cannot edit or view their own profile via the dashboard. It supports both PHP and React versions of the dashboard.
Setup
use Core\Services\DashboardService;
Common Use Cases
- Prevent the logged-in admin from editing or viewing their own profile via the dashboard.
- Retrieve a paginated list of users, excluding the current admin, with search, filters, and sorting.
- Count the users that match the current search and filters.
- Build props for the React version of the dashboard.
2. Public Methods Table of Contents
A. checkIfCurrentUser()
Prevents the current logged-in user from viewing or editing their own user record via the admin dashboard. If the given user matches the currently logged-in user, a danger flash message is set and a redirect occurs.
Parameters:
Users $user- The current user.string $redirect- The destination for redirect.
DashboardService::checkIfCurrentUser($user, 'admindashboard.users');
B. userFilter()
Builds the search and filter settings for the user list. The current user is always excluded.
- Search:
username,email,fname, andlname. - Filters: access level (
acl) using the levels in the ACL table, and status (inactive) usingSTATUS_OPTIONS. - Sorting:
username,acl,inactive,created_at, andupdated_at. The default is newest first.
Parameter:
object $request- The request object.
Returns:
SearchFilter- The filter for the user list.
$filter = DashboardService::userFilter($this->request);
C. paginateUsers()
Returns the users for the current page that match the filter.
Parameters:
Pagination $pagination- Instance of Pagination class.SearchFilter $filter- The filter fromuserFilter().
Returns:
array- An array of paginated users.
$users = DashboardService::paginateUsers($pagination, $filter);
D. totalUsers()
Returns the number of users that match the filter. Use it as the total when creating the Pagination object.
Parameter:
SearchFilter $filter- The filter fromuserFilter().
Returns:
int- The number of matching users.
$pagination = Pagination::fromRequest($this->request, 10, DashboardService::totalUsers($filter))
->withFilter($filter);
E. userList()
Builds the complete user list for the React dashboard: the current page of users, the pagination state, and the filter state, all as arrays. Used for both the first render and the JSON responses.
Parameters:
object $request- The request object.int $perPage- Users per page. The default is 10.
Returns:
array- An array with the keysusers,pagination, andfilter.
$list = DashboardService::userList($this->request);
F. userToArray()
Converts a user to an array for the React dashboard. Only the fields the table needs are included, so password hashes and tokens are never sent to the browser. Access levels are returned as a list, and dates include a “time ago” version.
Parameter:
Users $user- The user.
Returns:
array- The fieldsid,username,acl,inactive,created_at,updated_at,created_ago, andupdated_ago.
$users = array_map([DashboardService::class, 'userToArray'], $users);
G. optionList()
Converts a value => label array into a list of value and label pairs for React dropdowns. This keeps the order and avoids PHP turning '0' and '1' keys into a plain JSON list.
Parameter:
array $options- The options asvalue => label.
Returns:
array- A list of['value' => ..., 'label' => ...]arrays.
'statusOptions' => DashboardService::optionList(DashboardService::STATUS_OPTIONS),
H. STATUS_OPTIONS
The options for the status filter, as value => label:
public const STATUS_OPTIONS = [
'0' => 'Active',
'1' => 'Inactive',
];
3. Related ComponentsTable of Contents
AuthService::currentUser()– Used to identify and exclude the current user in queries.ACL::getOptionsForForm()– Provides the access levels for the access level filter.PaginationandSearchFilter– Handle paging, search, filters, and sorting. See the Pagination page.Users– Model for fetching and counting user records.
4. Notes Table of Contents
- All queries exclude the current logged-in user to prevent unintentional self-editing or display within administrative lists.
totalUserCountExceptCurrent()was replaced bytotalUsers(), which counts only the users that match the current search and filters.- See Supporting PHP and React Views Together for a controller that uses this service for both view types.
- This service is intentionally minimal and can be extended with additional dashboard-specific helper methods such as widget registration, statistics blocks, or activity feeds.