Skip to content

Conversation

@mariantrifu
Copy link

Q A
Branch? 4.2
Tickets Closes #7587 #7587
License MIT

API Platform library tries to extract the foreign_key from all relations, but BelongsToMany relations don't have a getForeignKeyName() method, so it returns null. When API Platform tries to flip an array containing null values, PHP throws the error: array_flip(): Can only flip string and integer values, entry skipped.

@mariantrifu mariantrifu changed the title fix(laravel): BelongsToMany relations dont have a getForeignKeyName() fix(laravel): belongs-to-many relations dont have a get-foreign-key-name method Dec 5, 2025
$relations = $this->getRelations($model);

$foreignKeys = array_flip(array_column($relations, 'foreign_key'));
$foreignKeys = array_flip(array_filter(array_column($relations, 'foreign_key')));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to have a functional test for this would you be able to add one? Models are inside src/Laravel/workbench/app/Models.
To run the tests see https://github.com/api-platform/core/blob/4a72073573af0740ffb24c7ed1e8a13e36546654/src/Laravel/CONTRIBUTING.md#tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants