1
投票表:フェッチデータ
id user id vote votable_id votable_type
1 4 5 1 App\Comment
2 2 4 4 App\Answer
....
回答やコメントモデル
public function votes()
{
return $this->morphOne('App\Vote', 'votable');
}
public function user()
{
return $this->belongsTo('App\User','user_id','id');
}
投票モデル:
public function votable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo('App\User','user_id','id');
}
ログインしたユーザーがユーザーモデルから投票した回答とコメントを取得するにはどうすればよいですか。
$user=User::where(['id'=>Auth::user()->id])->with('questions','answers','comments','comments.votes','votes')
->first();