Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codeXX's avatar

Many to Many relationship sync() error :array_values(): Argument #1 ($array) must be of type array, string given.

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?

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...')

0 likes
1 reply
codeXX's avatar
codeXX
OP
Best Answer
Level 1

Alright! While playing around. I changed the FK ID category_id to associate_product_category_id and it worked.

This could be because of a belongsTo relationship where I have a category_id in the fillable.

Which I find it odd because the category_id of belongsToMany will be present under the relationship name key in the document.

Anyway that solved it.

If anyone knows what caused it please do comment.

1 like

Please or to participate in this conversation.