I am a little bit confused about the API Resources.
What is the difference between having a RoleResource or just return it as json directly from controller?
Is this less performance going through the data like in the example bellow?
public function index(): JsonResponse
{
return response()->json(
Role::with(['permissions'])
->withCount('users')
->paginate(10)
->through(fn($role) => [
'id' => $role->id,
'title' => $role->title,
'permissions' => $role['permissions']->map(fn($permission) => [
'value' => $permission->id,
'label' => $permission->title
]),
'numberOfUsers' => $role->users_count
]);
);
}
or having a RoleResource is much better (speaking of performance). I am not sure behind the scenes, the RoleResource, I think is doing the same....going through all the data and arrange them as you want.
The fronted needs that structure for each entry.