From here, I want to put data from relationship to my API resources.
With one key and one relationship, it works. But, with two different key and two same relationship, it doesn't.
My goal is :
- If x relationship is loaded, use this data as key "a".
- If y relationship is loaded, use this data as key "a".
What have I tried
"Post" object with one key named "comments", whenLoaded "comments" relationship. (works)
"Post" object with two key named "comments" and "comments". First "comments" is created whenLoaded "comments" relationship and second "comments" is created whenLoaded "comments_by_tag" relationship.
Then, I load "Post" with "comments" relationship (failed). And load "comments_by_tag" relationship (works).
- "Post" object with two key named "comments" and "comments". Now, I change the position of those which second "comments" is the first and first "comments" is the second.
Then, I load "Post" with "comments" relationship (works). And load "comments_by_tag" relationship (failed).
This is my code on
return [
'comments' => CommentItem::collection($this->whenLoaded('comments')),
'comments' => CommentItem::collection($this->whenLoaded('comments_by_tag')),
];