Hi there,
I have an issue with appending attributes that are calculated based on relations.
Specifically, I have a model called User which represents the user to log in, and I have two models attached to the user for additional information. A user can either be a user with a profile or a user attached to a staff.
In the user model, I have a calculated attribute called is_profile_user (and also an attribute called is_staff_user, not included in the example code) for better readable code and also to provide this to the frontend. This is also the reason why I $append that data.
In the profile (and the staff) model, I have a calculated attribute called user_status to display whether that profile in the system already has a user attached and whether that one is active. This is also for the frontend; thus I also added that attribute to the $appends array.
When calling toArray or simply returning User::all() in a controller, this causes an infinite recursion. The same happens when calling jsonSerialize() (called by telescope).
This does not happen when calling attributesToArray() as this excludes the calculated attributes.
Example source code: https://github.com/irobin591/laravel-attribute-loop/commit/7641eee203407586da828d42872cea4ee93c39c3
Failing testcases because of infinite recursion:
https://github.com/irobin591/laravel-attribute-loop/actions/runs/10959054378/job/30430598531
I have no idea how to solve this issue, though.
Any ideas?