core icon indicating copy to clipboard operation
core copied to clipboard

Laravel package bug in ModelMetadata

Open mariantrifu opened this issue 2 months ago • 3 comments

API Platform Laravel package version(s) affected: 4.1.* and 4.2.*

Description
When a Laravel Model has a BelongsToMany relationship, I see this bug:

ErrorException vendor/api-platform/laravel/Eloquent/Metadata/ModelMetadata.php:80

array_flip(): Can only flip string and integer values, entry skipped

Laravel version: 12, php version 8.4, api-platform/laravel version 4.1 and 4.2

Possible Solution
In ModelMetadata, on line 80 there is this line: $foreignKeys = array_flip(array_column($relations, 'foreign_key')); I've changed it to this line: $foreignKeys = array_flip(array_filter(array_column($relations, 'foreign_key')));

It looks like 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 avatar Dec 04 '25 21:12 mariantrifu

Hi thanks! Would you be able to provide a pull request?

soyuka avatar Dec 05 '25 19:12 soyuka

I'll create a PR and let you know when ready.

mariantrifu avatar Dec 05 '25 21:12 mariantrifu

@soyuka this is the PR https://github.com/api-platform/core/pull/7589 , hope it's ok created.

mariantrifu avatar Dec 05 '25 22:12 mariantrifu

fixed with this PR https://github.com/api-platform/core/pull/7589

mariantrifu avatar Dec 17 '25 07:12 mariantrifu