1For example, a blog post may have an infinite number of comments. And a single
2comment belongs to only a single post
3
4class Post extends Model
5{
6 public function comments()
7 {
8 return $this->hasMany('App\Models\Comment');
9 }
10}
11
12class Comment extends Model
13{
14 public function post()
15 {
16 return $this->belongsTo('App\Models\Post');
17 }
18}
1// Detach a single role from the user...
2$user->roles()->detach($roleId);
3
4// Detach all roles from the user...
5$user->roles()->detach();
1$user = User::find(1); //any user we want to find
2$user->trophies()->attach($idOfTrophy);
3//pass id or array of a Trophy ids
4//suppose admin has selected the trophy from a form and trophy id
5// is in $request object, then.
6$trophyId = $request->trophy_id;
7$user->trophies()->attach($trophyId); //record is created in DB.
8attach and syncWithoutDetaching both does same job
1$user = User::find(1); //any user we want to find $user->trophies()->attach($idOfTrophy); //pass id or array of a Trophy ids //suppose admin has selected the trophy from a form and trophy id// is in $request object, then.$trophyId = $request->trophy_id;$user->trophies()->attach($trophyId); //record is created in DB.attach and syncWithoutDetaching both does same job
1$user->customviews()
2 ->newPivotStatement()
3 ->where('user_id', '=', $user->id)
4 ->update(array('default' => 0));