I have a many to many relationship and when I used sysnc() on this particular model I received the error array_values(): Argument #1 ($array) must be of type array, string given. I am supplying an array of IDs, I don't know what is causing this error. Is it a bug in Mongo library? has anyone faced an issue like this before?
class Product extends Model {
...
public function associate_product_categories() {
return $this->belongsToMany(Category::class, null, 'product_id', 'category_id');
}
}
class Category extends Model {
...
public function associate_products()
{
return $this->belongsToMany(Product::class, null, 'category_id', 'product_id');
}
}
// controller....
public function update() {
...
$categories = [
'...', '...', '...'
];
$product->associate_product_categories()->sync($categories);
}
The stack trace...
[2025-06-02 11:29:23] local.ERROR: array_values(): Argument #1 ($array) must be of type array, string given {"userId":"683058ac4c6f72774804ffe2","exception":"[object] (TypeError(code: 0): array_values(): Argument #1 ($array) must be of type array, string given at /var/www/project/api/vendor/mongodb/laravel-mongodb/src/Eloquent/DocumentModel.php:529)
[stacktrace]
#0 /var/www/project/api/vendor/mongodb/laravel-mongodb/src/Eloquent/DocumentModel.php(529): array_values('6830410a0bad670...')
#1 /var/www/project/api/vendor/mongodb/laravel-mongodb/src/Eloquent/DocumentModel.php(481): MongoDB\\Laravel\\Eloquent\\Model->pullAttributeValues('product_categor...', Array)
#2 /var/www/project/api/vendor/mongodb/laravel-mongodb/src/Relations/BelongsToMany.php(242): MongoDB\\Laravel\\Eloquent\\Model->pull('product_categor...', Array)
#3 /var/www/project/api/vendor/mongodb/laravel-mongodb/src/Relations/BelongsToMany.php(157): MongoDB\\Laravel\\Relations\\BelongsToMany->detach(Array)
#4 /var/www/project/api/app/Http/Controllers/Traits/ProductTrait.php(161): MongoDB\\Laravel\\Relations\\BelongsToMany->sync(Array)
#5 /var/www/project/api/app/Http/Controllers/Product/Master/UpdateController.php(33): App\\Http\\Controllers\\Product\\Master\\UpdateController->update_associate_categories(Object(App\\Models\\Product\\ProductMaster), Array)
#6 /var/www/project/api/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(46): App\\Http\\Controllers\\Product\\Master\\UpdateController->ctrl_update_associate_categories(Object(Illuminate\\Http\\Request), 'first-business', 'product-name-1')
#7 /var/www/project/api/vendor/laravel/framework/src/Illuminate/Routing/Route.php(265): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\Product\\Master\\UpdateController), 'ctrl_update_ass...')