Repository: laravel/framework
File: src/Illuminate/Http/Resources/JsonApi/Concerns/ResolvesJsonApiElements.php (PHP)

Code before:
        $relations = new Collection;

        dump($this->loadedRelationshipsMap);

        foreach ($this->loadedRelationshipsMap as $value) {
            [$resourceInstance, $type, $id, $isUnique] = $value;

            if (! $resourceInstance instanceof JsonApiResource &&
                $resourceInstance instanceof JsonResource) {
                $resourceInstance = new JsonApiResource($resourceInstance->resource);
            }

            $relationsData = $resourceInstance->withoutRequestQueryString()->withIncludedFromLoadedRelationships()->resolve($request);

            $relations->push(array_filter([
                'id' => $id,
                'type' => $type,
                '_uniqueKey' => $isUnique === true ? [$id, $type] : [$id, $type, (string) Str::random()],
                'attributes' => Arr::get($relationsData, 'data.attributes'),
                'relationships' => Arr::get($relationsData, 'data.relationships'),
                'links' => Arr::get($relationsData, 'data.links'),
                'meta' => Arr::get($relationsData, 'data.meta'),
            ]));
        }

        return $relations->dump()->uniqueStrict(fn ($relation) => $relation['_uniqueKey'])
            ->map(fn ($relation) => Arr::except($relation, ['_uniqueKey']))
            ->all();
    }

    /**
     * Resolve the links for the resource.
     *
     * @return array<string, mixed>
     */
    protected function resolveResourceLinks(JsonApiRequest $request): array
    {
        return $this->toLinks($request);
    }

    /**
     * Resolve the meta information for the resource.
     *
     * @return array<string, mixed>
     */
    protected function resolveResourceMetaInformation(JsonApiRequest $request): array
    {
        return $this->toMeta($request);
    }

    /**

Review comment (suggestion):
```suggestion
        return $relations->uniqueStrict(fn ($relation) => $relation['_uniqueKey'])
```

Code after:
        $index = 0;

        while ($index < count($this->loadedRelationshipsMap)) {
            [$resourceInstance, $type, $id, $isUnique] = $this->loadedRelationshipsMap[$index];

            if (! $resourceInstance instanceof JsonApiResource &&
                $resourceInstance instanceof JsonResource) {
                $resourceInstance = new JsonApiResource($resourceInstance->resource);
            }

            $relationsData = $resourceInstance->withoutRequestQueryString()->withIncludedFromLoadedRelationships()->resolve($request);

            array_push($this->loadedRelationshipsMap, ...$resourceInstance->loadedRelationshipsMap);

            $relations->push(array_filter([
                'id' => $id,
                'type' => $type,
                '_uniqueKey' => $isUnique === true ? [$id, $type] : [$id, $type, (string) Str::random()],
                'attributes' => Arr::get($relationsData, 'data.attributes'),
                'relationships' => Arr::get($relationsData, 'data.relationships'),
                'links' => Arr::get($relationsData, 'data.links'),
                'meta' => Arr::get($relationsData, 'data.meta'),
            ]));

            $index++;
        }

        return $relations->uniqueStrict(fn ($relation) => $relation['_uniqueKey'])
            ->map(fn ($relation) => Arr::except($relation, ['_uniqueKey']))
            ->all();
    }

    /**
     * Resolve the links for the resource.
     *
     * @return array<string, mixed>
     */
    protected function resolveResourceLinks(JsonApiRequest $request): array
    {
        return $this->toLinks($request);
    }

    /**
     * Resolve the meta information for the resource.
     *
     * @return array<string, mixed>
     */
    protected function resolveResourceMetaInformation(JsonApiRequest $request): array
    {
        return $this->toMeta($request);
    }