Skip to content

Conversation

@mariantrifu
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll try to add that also.

@MakFly
Copy link

MakFly commented Dec 8, 2025

Hi , i have same problem , when do you think you can merge ? this fix ?

@soyuka

@soyuka
Copy link
Member

soyuka commented Dec 8, 2025

sure thing is I need to add a test you can also just drop an example of model and request I'll add it (though any llm can do it inside the workbench and the functional tests)

@soyuka soyuka merged commit bc1fd4c into api-platform:4.2 Dec 11, 2025
129 of 130 checks passed
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.

3 participants