staudenmeir/eloquent-json-relations

str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

ainesophaur opened this issue · 4 comments

When working with a belongsToJson relationship, it seems InteractsWithPivotRecords performs a depreciated operation when calling str_replace on line 102. Everything works, but the PHP Depreciated notice is thrown

PHP Deprecated:  str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/html/vendor/staudenmeir/eloquent-json-relations/src/Relations/InteractsWithPivotRecords.php on line 102

The operation that PHP complains about:
$key = str_replace('->', '.', $this->key);

where $key is constructed in IsJsonRelationship to support either an array of ids or array of objects.

In my setup, User morphsOne TicketBoardPreference, and TicketBoardPreference has a belongsToJson relationship preferredTicketBoards to TicketBoard via fk preferences->ticket_boards

If I attempt to attach, toggle or detach a TicketBoard from TicketBoardPreferences the operation works properly, but I receive a PHP Depreciated notice. Based on the code I see in IsJsonRelationship, I believe this notice would only trigger when doing an array of ids, but not an array of objects.

 User::first()->ticketBoardPreferences->preferredTicketBoards()->toggle(TicketBoard::whereKey(9356512388585053)->first())

PHP Deprecated:  str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/html/vendor/staudenmeir/eloquent-json-relations/src/Relations/InteractsWithPivotRecords.php on line 102

TicketBoard

public function preferredByPreference(): HasManyJson
{
    return $this->hasManyJson(related: TicketBoardPreference::class, foreignKey: 'preferences->ticket_boards');
}

TicketBoardPreference

protected $fillable = ['preferences->ticket_boards'];
protected $casts = ['preferences' => 'json'];

public function preferredTicketBoards(): BelongsToJson
{
    return $this->belongsToJson(related: TicketBoard::class, foreignKey: 'preferences->ticket_boards');
}

Thanks, I'll take a look.

I released a fix for this issue. Weird that the test runners in GitHub Actions don't show these deprecation notices.

Appreciate it :) FWIW, I would only see the warning when using tinker

Thanks