laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

`syncWithoutDetaching()` isn't accepting a single model as an argument

Open mrneatly opened this issue 2 years ago • 0 comments

  • Laravel Version: 9.13.0
  • Laravel-mongodb Version: 3.9.0
  • PHP Version: 8.0.17
  • Database Driver & Version: MongoDB PHP, v5.0.6

Description:

When passing a model instance to a syncWithoutDetaching() method, an exception is thrown. This behaviour differs from cases with Laravel's Eloquent models, when a single model is accepted by this method.

Steps to reproduce

Let's use a User and a Post entity for this example

  1. Create a Post model
  2. Create a User model with a one-to-many relation to Post one
  3. Store a record of a single user in DB (without any related posts)
  4. Store a record of a single post in DB
  5. Pick a post record from a previous step via Post model
  6. Try to assign this post to a user like this: $user->posts()->syncWithoutDetaching($post);

Expected behaviour

An existing post record should be put in a relationship with a user record

Actual behaviour

An exception is thrown instead:

Jenssegers\Mongodb\Relations\BelongsToMany::formatSyncList(): Argument #1 ($records) must be of type array, App\Models\Representative given

mrneatly avatar May 28 '22 08:05 mrneatly