我的应用程序允许用户同时使用morphMany关系同时发布帖子和评论
最喜欢的型号:
public function favorited()
{
return $this->morphTo();
}
public function user(){
return $this->belongsTo(User::class, 'user_id');
}
我的特质
trait Favoriteable
{
public function favorites()
{
return $this->morphMany(Favorite::class, 'favorited');
}
}
我的帖子
public function favorites()
{
return $this->morphMany(Favorite::class, 'favorited');
}
我的评论
public function favorites()
{
return $this->morphMany(Favorite::class, 'favorited');
}
我的用户
public function favorites(){
return $this->hasMany(Favorite::class);
}
现在在刀片我试图让用户收藏的活动..我尝试了$活动 - > subject->收藏了更多没什么作用但是当我dd($ activity-> subject-> favited)时我明白了:
Post {#682 ▼
#guarded: []
#fillable: array:4 [▶]
#appends: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#original: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#casts: []
#dates: []
#dateFormat: null
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
}
当我dd($ activity->结果)时:
Favorite {#685 ▼
#guarded: []
#with: array:1 [▼
0 => "favorited"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▼
"id" => 30
"user_id" => 3
"favorited_id" => 14
"favorited_type" => "App\Post"
"created_at" => "2018-05-18 16:01:56"
"updated_at" => "2018-05-18 16:01:56"
]
#original: array:6 [▼
"id" => 30
"user_id" => 3
"favorited_id" => 14
"favorited_type" => "App\Post"
"created_at" => "2018-05-18 16:01:56"
"updated_at" => "2018-05-18 16:01:56"
]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:1 [▼
"favorited" => Post {#720 ▼
#guarded: []
#fillable: array:4 [▶]
#appends: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#original: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#casts: []
#dates: []
#dateFormat: null
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}