いいねしたユーザーの一覧を表示

This commit is contained in:
shibafu
2019-04-07 23:27:24 +09:00
parent 2cd09402d1
commit 4ed2a9048d
5 changed files with 59 additions and 7 deletions

View File

@@ -46,14 +46,23 @@ class Ejaculation extends Model
{
if (Auth::check()) {
// (ejaculation_id, user_id) でユニークなわけですが、サブクエリ発行させるのとLeft JoinしてNULLかどうかで結果を見るのどっちがいいんでしょうね
return $query->withCount([
'likes',
'likes as is_liked' => function ($query) {
$query->where('user_id', Auth::id());
}
]);
return $query
->with(['likes.user' => function ($query) {
$query->where('is_protected', false)
->orWhere('id', Auth::id());
}])
->withCount([
'likes',
'likes as is_liked' => function ($query) {
$query->where('user_id', Auth::id());
}
]);
} else {
return $query->withCount('likes')
return $query
->with(['likes.user' => function ($query) {
$query->where('is_protected', false);
}])
->withCount('likes')
->addSelect('0 as is_liked');
}
}