in the place of manually establishing the post_id characteristic on the Comment model you may possibly place the remark utilizing the relationship’s conserve technique:
Keep in mind that we did not access the reviews relationship as a property that is dynamic. Rather, we called the remarks solution to get an example of this relationship. The salvage method will immediately add the correct post_id value to this new Comment model.
You may use the saveMany method if you need to save multiple related models:
The salvage and methods that are saveMany maybe not include the brand new models to your in-memory relationships which are currently packed onto the moms and dad model. You may wish to use the refresh method to reload the model and its relationships if you plan on accessing the relationship after using the save or saveMany methods:
Recursively Preserving Versions & Relationships
You may use the push method if you would like to save your model and all of its associated relationships. The Post model will be saved as well as its comments and the comment’s authors in this example
The make technique
Besides the salvage and methods that are saveMany it’s also possible to make use of the make technique, which takes a range of characteristics, produces a model, and inserts it in to the database. The essential difference between salvage and produce is the fact that save accepts a full Eloquent model example while make takes a plain PHP array . The newly produced model will be came back by the make technique:
You may possibly use the method that is createMany create multiple relevant models:
You may make use of the findOrNew , firstOrNew , firstOrCreate , and methods that are updateOrCreate create and upgrade models on relationships.