Given the following models:
Car Model
- parts: hasMany(Parts::class)
Part Model
- car: belongsTo(Car::class)
Running
Car::find(1)->parts()->saveMany(Part::hydrate($request->parts));
properly returns a list of Parts, 3 have id, one does not, so that's 3 models that need to be updated and one needs to be inserted.
However looking at my database, not a single row is inserted and issuing dd(Car::find(1)->parts)
returns an empty array. createMany
works, but that does not update the models that have an id
- instead it creates new ones.
From the documentation,
If you need to save multiple related models, you may use the saveMany method
...
the difference between
save
andcreate
is thatsave
accepts a full Eloquent model instance while create accepts a plain PHParray
So what is the reason createMany
works but saveMany
does not?