laravel-mongodb
laravel-mongodb copied to clipboard
Eloquent\Model->drop() doesn't return int
- Laravel-mongodb Version: 3.6.3
- PHP Version: 7.3.19
- Database Driver & Version: mongodb 1.7.5
Description:
The drop()
method of the Jenssegers\Mongodb\Eloquent\Model
class has a docblock that says the returning value is an int
, but the actual value is being returned is an instance of the Jenssegers\Mongodb\Eloquent\Builder
class.
This is because the drop()
method is returning the resulting value of the unset()
method of the Jenssegers\Mongodb\Eloquent\Builder
instance. But this call to the unset()
method is being forwarded to the Jenssegers\Mongodb\Query\Builder
class by the Illuminate\Database\Eloquent::__call()
magic method, which means it returns the Jenssegers\Mongodb\Eloquent\Builder
instance.
This can be fixed by adding a __call()
method to the Jenssegers\Mongodb\Eloquent\Builder
class, that can be like this:
/**
* @inheritdoc
*/
public function __call($method, $parameters)
{
if ($method == 'unset') {
return call_user_func_array([$this->query, 'drop'], $parameters);
}
return parent::__call($method, $parameters);
}
Similar to the _call()
method of Jenssegers\Mongodb\Eloquent\Model
and Jenssegers\Mongodb\Query\Builder
classes.
Steps to reproduce
- Get a
Jenssegers\Mongodb\Eloquent\Model
instance. - Try to drop an attribute of this model instance by calling the
drop($attribute)
method. - The attribute is successfully dropped, but the returning value of the
drop()
method is an instance of theJenssegers\Mongodb\Eloquent\Builder
class instead of aninteger
.
Expected behaviour
The drop()
method of the Jenssegers\Mongodb\Eloquent\Model
class should return an int
value.
Actual behaviour
The drop()
method of the Jenssegers\Mongodb\Eloquent\Model
class is returning a Jenssegers\Mongodb\Eloquent\Builder
instance.
Actually, a best solution would be add 'unset'
to the protected passthru
array property of the Jenssegers\Mongodb\Eloquent\Builder
class.
Don't want to open separate issue: Builder->drop also doesn't support additional $options param for performUpdate(), like Builder->update for example, so we are unable to pass conditional option to remove some nested array like ['arrayFilter' => something]