kirkbushell / eloquence

A drop-in library for certain database functionality in Laravel, that allows for extra features that may never make it into the main project.
MIT License
545 stars 58 forks source link

Count cache support nullable #118

Closed fmarquesto closed 4 months ago

fmarquesto commented 4 months ago

When we use CountedBy on a nullable field and we set this to null:

...
$table->integer('user_id')->nullable();
...

$post->userId = null;

We receive an unexpected error: TypeError : Eloquence\Behaviours\CacheConfig::relatedModel(): Return value must be of type Illuminate\Database\Eloquent\Model, null returned

With this PR I've added a validation to avoid retrieving the model on the update method if the foreignKey value is null.