Skip to content

HasOneOrMany - DEPRECATED Using null as an array offset is deprecated #58151

@Mishanki

Description

@Mishanki

Laravel Version

12.43.1

PHP Version

8.5

Database Driver & Version

Postgres 16

Description

DEPRECATED Using null as an array offset is deprecated, use an empty string instead in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php on line 160.

Steps To Reproduce

Sometimes parent_guid is null and I have DEPRECATED warning

public function parent(): HasMany
{
        return $this->hasMany(self::class, 'external_guid', 'parent_guid');
}

Deprecated code (HasOneOrMany.php)

protected function matchOneOrMany(array $models, EloquentCollection $results, $relation, $type)
    {
        $dictionary = $this->buildDictionary($results);
        //...
        foreach ($models as $model) {
           // When $key is null - DEPRECATED warning
            if (isset($dictionary[$key = $this->getDictionaryKey($model->getAttribute($this->localKey))])) {
              //...
            }
        }

        return $models;
    }

Updated code (HasOneOrMany.php)

protected function matchOneOrMany(array $models, EloquentCollection $results, $relation, $type)
    {
        $dictionary = $this->buildDictionary($results);
        //...
        foreach ($models as $model) {
            $key = $this->getDictionaryKey($model->getAttribute($this->localKey));
            if ($key === null) {
                $key = '';
            }
            if (isset($dictionary[$key])) {
                //...
            }
        }

        return $models;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions