Laravel package bug in ModelMetadata
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.
Hi thanks! Would you be able to provide a pull request?
I'll create a PR and let you know when ready.
@soyuka this is the PR https://github.com/api-platform/core/pull/7589 , hope it's ok created.
fixed with this PR https://github.com/api-platform/core/pull/7589