Define belongsTo relationship
Do imagine a "Post" model that belongs to "User" model. This can either be a "one to one" or "one to many" definition.
- User => has one => Post
- User => has many => Post
- Post => belongs to => User
How to associate a Model
To associate a model you can call the "associate" method on the relationship.
<?php $firstUser = User::first(); Post::first()->user()->associate($firstUser)->save();
How to dissociate a Model
To dissociate a model you can call the "dissociate" method on the relationship.
<?php $firstUser = User::first(); Post::first()->user()->dissociate($firstUser)->save();
Leave a reply