Skip to main content

RoleResource

Transforms a Role model into a JSON-serializable array for API responses, including optional relations and an ancestors projection derived from loaded paths.

Namespace

JobMetric\Rolix\Http\Resources\RoleResource

Overview

RoleResource:

  • Exposes core role fields with normalized allow / deny arrays
  • Formats timestamps as ISO 8601 via ?->toISOString()
  • Loads nested relations only when eager-loaded (whenLoaded)
  • Builds a compact ancestors list from loaded paths (levels > 0)

Resource Structure

{
"id": 1,
"type": "system",
"parent_id": null,
"name": "Administrator",
"description": "Full system access",
"allow": ["users.view", "users.update"],
"deny": [],
"is_default": false,
"is_super": true,
"ordering": 0,
"created_at": "2024-01-01T12:00:00.000000Z",
"updated_at": "2024-01-01T12:00:00.000000Z"
}

With relations loaded:

{
"id": 2,
"type": "system",
"parent_id": 1,
"name": "Editor",
"description": null,
"allow": ["posts.update"],
"deny": ["posts.delete"],
"is_default": false,
"is_super": false,
"ordering": 10,
"created_at": "2024-01-02T09:00:00.000000Z",
"updated_at": "2024-01-02T09:00:00.000000Z",
"parent": {
"id": 1,
"type": "system",
"parent_id": null,
"name": "Administrator",
"description": "Full system access",
"allow": [],
"deny": [],
"is_default": false,
"is_super": true,
"ordering": 0,
"created_at": "2024-01-01T12:00:00.000000Z",
"updated_at": "2024-01-01T12:00:00.000000Z"
},
"children": [],
"rules": [
{
"id": 5,
"role_id": 2,
"driver": "time",
"payload": { "from": "09:00", "to": "18:00" },
"created_at": "2024-01-02T09:00:00.000000Z",
"updated_at": "2024-01-02T09:00:00.000000Z"
}
],
"paths": [
{
"type": "system",
"role_id": 2,
"path_id": 2,
"level": 0
},
{
"type": "system",
"role_id": 2,
"path_id": 1,
"level": 1,
"path": { "id": 1, "name": "Administrator", "type": "system" }
}
],
"ancestors": [
{ "id": 1, "name": "Administrator", "type": "system", "level": 1 }
]
}

Field Details

Complete Field Reference

FieldTypeAlways presentDescriptionExample
idintegeryesRole primary key1
typestringyesRole type registry key"system"
parent_idinteger|nullyesParent role idnull
namestringyesDisplay name"Editor"
descriptionstring|nullyesOptional description"Can edit posts"
allowarray<string>yesAllowed permissions (null[])["posts.view"]
denyarray<string>yesDenied permissions (null[])[]
is_defaultbooleanyesDefault role for the typefalse
is_superbooleanyesSuper role flagfalse
orderingintegeryesSort order10
created_atstring|nullyesISO 8601 timestamp"2024-01-01T12:00:00.000000Z"
updated_atstring|nullyesISO 8601 timestamp"2024-01-01T12:00:00.000000Z"
parentobjectwhenLoadedNested RoleResource
childrenarraywhenLoadedCollection of RoleResource
rulesarraywhenLoadedRoleRuleResource collection
pathsarraywhenLoadedRolePathResource collection
ancestorsarraywhenLoaded(paths)Compact ancestor rows from paths with level > 0

whenLoaded Relations

JSON keyRelationResource
parentparentRoleResource::make
childrenchildrenRoleResource::collection
rulesrulesRoleRuleResource::collection
pathspathsRolePathResource::collection
ancestorsderived from pathsInline { id, name, type, level } sorted by level

ancestors requires paths to be loaded. Each ancestor role is taken from $path->path (uses the relation if loaded, otherwise queries). Null ancestors are filtered out.

Basic Usage

use JobMetric\Rolix\Models\Role;
use JobMetric\Rolix\Http\Resources\RoleResource;

$role = Role::with(['parent', 'children', 'rules', 'paths.path'])->find(1);

return RoleResource::make($role);
return RoleResource::collection(Role::query()->ofType('system')->paginate(20));

Service Integration

use JobMetric\Rolix\Facades\Role;
use JobMetric\Rolix\Http\Resources\RoleResource;

$response = Role::store([
'name' => 'Editor',
'type' => 'system',
'allow' => ['posts.update'],
]);

if ($response->isSuccess()) {
return RoleResource::make($response->getData())
->response()
->setStatusCode(201);
}