1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class User extends Model
8{
9 /**
10 * Get the phone associated with the user.
11 */
12 public function phone()
13 {
14 return $this->hasOne(Phone::class);
15 }
16}
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$user->roles()->attach($roleIds);
2$user->roles()->detach($roleIds);
3$user->roles()->sync($roleIds);
4$user->roles()->toggle($roleIds);
1class Post extends Model
2{
3 // 1 Post has many comments
4 public function comments()
5 {
6 return $this->hasMany(Comment::class);
7 }
8}
1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Post extends Model
8{
9 /**
10 * Get the comments for the blog post.
11 */
12 public function comments()
13 {
14 return $this->hasMany(Comment::class);
15 }
16}