Laravel-mongodb: Issue with ManyToMany relations detach all on Laravel 5.2

Created on 15 Sep 2016  路  1Comment  路  Source: jenssegers/laravel-mongodb

Hi,

The detach() with empty args suppose to delete all relations but it leaves junk data on the one side of relation.

Example :
We have a collection of books and a collection of authors and we want to relate them ManyToMany.
So books collection gonna have a field "author_ids" which is an array of ids and
authors also gonna have a field "book_ids" which is an array of ids.

When someone do $bookModel->authors()->detach(); all the relations of book with authors will be deleted.
But this clears only the field "book_ids" at authors collection leaving the data at field "authors_ids" at books collection intact.

@jenssegers can you confirm the issue?

Thanks leave a comment on how to resolve the issue

Most helpful comment

I find a way to work around this issue
So instead of using the $bookModel->authors()->detach();
you can use $bookModel->authors()->sync([]);

Note
This is working because this library has implement a sync() function for mongodb
but the detach() function is using Laravel's implementation so i think this is happening
because the Laravel's implementation use a pivot table for ManyToMany relations.

>All comments

I find a way to work around this issue
So instead of using the $bookModel->authors()->detach();
you can use $bookModel->authors()->sync([]);

Note
This is working because this library has implement a sync() function for mongodb
but the detach() function is using Laravel's implementation so i think this is happening
because the Laravel's implementation use a pivot table for ManyToMany relations.

Was this page helpful?
0 / 5 - 0 ratings