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<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Phone extends Model
8{
9 /**
10 * Get the user that owns the phone.
11 */
12 public function user()
13 {
14 return $this->belongsTo('App\Models\User');
15 }
16}
1use App\Models\User;
2
3$user = User::find(1);
4
5$user->roles()->attach($roleId);
1$movies = Movie::whereHas('director', function($q) {
2 $q->where('name', 'great');
3})->get();
4