Skip to content

Laravel package bug in ModelMetadata #7587

@mariantrifu

Description

@mariantrifu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions