Eloquent provides methods that are convenient incorporating brand new models to relationships. As an example, perchance you have to include a new comment to a post.

Eloquent provides methods that are convenient incorporating brand new models to relationships. As an example, perchance you have to include a new comment to a post.

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.

Belongs To Relationships

If you want to designate a kid model to a different moms and dad model, you could use the connect technique. The User model defines a belongsTo relationship to the Account model in this example. This connect technique will set the international key from the son or daughter model:

To get rid of a parent model from a young child model, you’ll utilize the method that is dissociate. This process will set the connection’s international key to null :

Numerous to relationships that are many

Attaching / Detaching

Eloquent also provides techniques to make working together with many-to-many relationships far more convenient. For instance, why don’t we imagine a person might have numerous functions and a task might have numerous users escort review Anchorage. You may make use of the attach solution to connect a task to a person by placing an archive when you look at the relationship’s intermediate dining table:

Whenever connecting a relationship to a model, it’s also possible to pass a myriad of extra information become inserted in to the table that is intermediate

Often it may be required to eliminate a task from a person. To get rid of a many-to-many relationship record, utilize the detach method. The detach technique will delete the appropriate record out associated with the intermediate dining table; but, both models will stay into the database:

For convenience, attach and detach also accept arrays of IDs as input:

Syncing Associations

You may even utilize the sync solution to build many-to-many associations. The sync technique takes an array of IDs to put regarding the table that is intermediate. Any IDs which are not when you look at the provided array are going to be taken out of the table that is intermediate. So, following this procedure is complete, only the IDs into the provided array will occur into the intermediate table:

It’s also possible to pass extra table that is intermediate because of the IDs:

You may use the syncWithoutDetaching method if you do not want to detach existing IDs that are missing from the given array:

Toggling Associations

The many-to-many relationship additionally provides a toggle technique which “toggles” the accessory status associated with the provided related model IDs. In the event that offered ID happens to be connected, it will be detached. Likewise, it will be attached if it is currently detached:

Upgrading An Archive Regarding The Intermediate Dining Table

If you wish to upgrade a current line in your relationship’s intermediate dining table, you could utilize the updateExistingPivot method. This process takes the record that is intermediate key and a range of characteristics to upgrade:

Touching Parent Timestamps

Whenever a model defines a belongsTo or belongsToMany relationship to another model, such as for example a Comment which belongs to a Post , it really is often beneficial to upgrade the moms and dad’s timestamp as soon as the youngster model is updated.

For instance, when a Comment model is updated, you might want to immediately “touch” the updated_at timestamp associated with the owning Post such that it is defined towards the date that is current time. To do this, you could include a touches home to your son or daughter model containing the names of this relationships which should have their updated_at timestamps updated once the son or daughter model is updated:

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *